SPL之AccessArray
<?php /**
* Class MyArrayAccess
* 提供像访问数组一样访问对象的能力的接口
*/
class MyArrayAccess implements ArrayAccess
{
private $container; public function __construct(Array $arr)
{
$this->container = $arr;
} // 某键是否存在 返回布尔值
public function offsetExists($offset)
{
var_dump(__METHOD__);
return isset($this->container[$offset]);
} // 获取键对应的值 返回值mixed
public function offsetGet($offset)
{
var_dump(__METHOD__);
return isset($this->container[$offset]) ? $this->container[$offset] : null;
} // 赋值
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
$this->container[] = $value;
} else {
$this->container[$offset] = $value;
}
var_dump(__METHOD__);
} // 删除键值
public function offsetUnset($offset)
{
unset($this->container[$offset]);
var_dump(__METHOD__);
} } 运行:
string(27) "MyArrayAccess::offsetExists"
bool(true)
string(24) "MyArrayAccess::offsetGet"
int(2)
string(26) "MyArrayAccess::offsetUnset"
string(27) "MyArrayAccess::offsetExists"
bool(false)
string(24) "MyArrayAccess::offsetSet"
string(24) "MyArrayAccess::offsetGet"
string(7) "A value"
string(24) "MyArrayAccess::offsetGet"
A valuestring(24) "MyArrayAccess::offsetSet"
string(24) "MyArrayAccess::offsetSet"
string(24) "MyArrayAccess::offsetSet"
object(MyArrayAccess)#1 (1) {
["container":"MyArrayAccess":private]=>
array(6) {
["one"]=>
int(1)
["three"]=>
int(3)
["two"]=>
string(7) "A value"
[0]=>
string(8) "Append 1"
[1]=>
string(8) "Append 2"
[2]=>
string(8) "Append 3"
}
}
<?php /**
* 单例
* 四私一公
*/
trait Singleton
{
static private $instance; private function __construct()
{ } private function __clone()
{ } private function __wakeup()
{ } static public function getInstance()
{
if (is_null(static::$instance)) {
$class = new ReflectionClass(get_called_class());
static::$instance = $class->newInstanceWithoutConstructor();
$method = $class->getConstructor();
$method->setAccessible(true);
$method->invokeArgs(static::$instance, func_get_args());
}
return static::$instance;
}
} /**
* 对象当数组用
*/
class myAccess implements ArrayAccess
{
use Singleton; private $dataSource = []; /**
* 可返回任意类型
*/
public function offsetGet($offset)
{
var_dump(__METHOD__);
return $this->dataSource[$offset] ?? null;
} /**
* 无返回值
*/
public function offsetSet($offset, $value)
{
var_dump(__METHOD__);
$this->dataSource[$offset] = $value;
} /**
* 返回布尔值
* 如果用empty()检测且返回值为true,则自动调用offsetGet
*/
public function offsetExists($offset)
{
var_dump(__METHOD__);
return isset($this->dataSource[$offset]);
} /**
* 无返回值
*/
public function offsetUnset($offset)
{
var_dump(__METHOD__);
unset($this->dataSource[$offset]);
}
} // use
echo '<pre>'; $obj = myAccess::getInstance();
$obj['name'] = 'tom';
isset($obj['name']);
empty($obj['name']);
unset($obj['name']);
var_dump($obj);
SPL之AccessArray的更多相关文章
- PHP 高级编程(4/5) - SPL异常类之 LogicException 逻辑异常
SPL 提供了一系列标准异常.日常的使用中我们应该根据需求科学的使用它们,来使我们的程序更加健壮.LogicException 是从 Exception 基类派生的,没有添加任何附加方法.抛出逻辑异常 ...
- PHP 高级编程(3/5) - 使用SPL(标准PHP库)实现观察者模式
SPL(标准PHP库 - Standard PHP Library)是PHP5面向对象功能中重要的部分.原文解释是这样的“The Standard PHP Library (SPL) is a col ...
- u-boot-2015.04 在tq2440上的移植(使用spl引导u-boot)
本次移植跟以往的不同之处是采用了spl来引导u-boot,参考了博客http://blog.csdn.net/fulinus/article/details/42738641 下载链接:http:// ...
- PHP SPL(PHP 标准库)
一.什么是SPL? SPL是用于解决典型问题(standard problems)的一组接口与类的集合.(出自:http://php.net/manual/zh/intro.spl.php) SPL, ...
- 【夯实PHP基础】PHP标准库 SPL
PHP SPL笔记 这几天,我在学习PHP语言中的SPL. 这个东西应该属于PHP中的高级内容,看上去很复杂,但是非常有用,所以我做了长篇笔记.不然记不住,以后要用的时候,还是要从头学起. 由于这是供 ...
- U-boot的目录结构及spl功能
转 http://tieba.baidu.com/p/2836672721 对uboot-2010.06及其以后的版本,将体系结构相关的内容合并,增加include文件夹,分离出通用库文件lib,其各 ...
- PHP标准库 (SPL) 笔记
简介 SPL是Standard PHP Library(PHP标准库)的缩写. The Standard PHP Library (SPL) is a collection of interfaces ...
- php SPL学习
数据结构 SplDoublyLinkedList - 该SplDoublyLinkedList类提供了一个双向链表的主要功能 SplStack - 该SplStack类提供了一种使用双向链表实现栈的主 ...
- PHP 标准库 SPL 之数据结构栈(SplStack)简单实践
PHP 5.3.0 版本及以上的堆栈描述可以使用标准库 SPL 中的 SplStack class,SplStack 类继承双链表 ( SplDoublyLinkedList ) 实现栈. 代码: & ...
随机推荐
- linux内核发生Oops时怎么办?
1. 定位发生Oops的代码 1.1 通过addr2line命令定位 aarch64-openwrt-linux-gnu-addr2line -e vmlinux ffff000008087f00 1 ...
- 再谈 tp的 实例化 类 的自动加载
表示一个域名下的所有/任何主机 使用 的格式是: [*.] example.com 其中 , example.com叫着 裸域名. (这个example.com/net/org不能被注册, 被保留) ...
- 抠图|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)
样例输入: 3 4 5 1 0 0 0 1 1 0 1 0 1 1 0 1 0 1 1 0 0 0 1 5 6 1 1 1 1 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 0 0 0 ...
- POJ 1873 The Fortified Forest(凸包)题解
题意:二维平面有一堆点,每个点有价值v和删掉这个点能得到的长度l,问你删掉最少的价值能把剩余点围起来,价值一样求删掉的点最少 思路:n<=15,那么直接遍历2^15,判断每种情况.这里要优化一下 ...
- C# 里调用vb的inputbox弹出窗
https://blog.csdn.net/hutao1101175783/article/details/16800871 先对项目添加对Microsoft.VisualBasic的引用 Inter ...
- Windows10状态栏右下角的上升三角号没有了
闲着没事装了360和电脑管家,捣鼓了下里面的功能,好像是弄了桌面整理和主题之后出现了这个问题,刚开始还以为因为清理卸载软件把系统的这项功能给卸载了,其实原因很简单,相信看完解决这个问题的方案你就明白了 ...
- redis事务之watch
三.redis事务之watch 首先要了解redis事务中watch的作用,watch命令可以监控一个或多个键,一旦其中有一个键被修改(或删除),之后的事务就不会执行.监控一直持续到exec命令(事务 ...
- Docker4之Stack
Make sure you have published the friendlyhello image you created by pushing it to a registry. We’ll ...
- ifconfig 输出里没有IP地址
转载: http://blog.csdn.net/johnstrive/article/details/5625121 inet addr:....Bcast:.....Mask:255.255.25 ...
- PTA 7-1 整数分解为若干项之和(20 分)
7-1 整数分解为若干项之和(20 分) 将一个正整数N分解成几个正整数相加,可以有多种分解方法,例如7=6+1,7=5+2,7=5+1+1,….编程求出正整数N的所有整数分解式子. 输入格式: 每个 ...