<?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的更多相关文章

  1. PHP 高级编程(4/5) - SPL异常类之 LogicException 逻辑异常

    SPL 提供了一系列标准异常.日常的使用中我们应该根据需求科学的使用它们,来使我们的程序更加健壮.LogicException 是从 Exception 基类派生的,没有添加任何附加方法.抛出逻辑异常 ...

  2. PHP 高级编程(3/5) - 使用SPL(标准PHP库)实现观察者模式

    SPL(标准PHP库 - Standard PHP Library)是PHP5面向对象功能中重要的部分.原文解释是这样的“The Standard PHP Library (SPL) is a col ...

  3. u-boot-2015.04 在tq2440上的移植(使用spl引导u-boot)

    本次移植跟以往的不同之处是采用了spl来引导u-boot,参考了博客http://blog.csdn.net/fulinus/article/details/42738641 下载链接:http:// ...

  4. PHP SPL(PHP 标准库)

    一.什么是SPL? SPL是用于解决典型问题(standard problems)的一组接口与类的集合.(出自:http://php.net/manual/zh/intro.spl.php) SPL, ...

  5. 【夯实PHP基础】PHP标准库 SPL

    PHP SPL笔记 这几天,我在学习PHP语言中的SPL. 这个东西应该属于PHP中的高级内容,看上去很复杂,但是非常有用,所以我做了长篇笔记.不然记不住,以后要用的时候,还是要从头学起. 由于这是供 ...

  6. U-boot的目录结构及spl功能

    转 http://tieba.baidu.com/p/2836672721 对uboot-2010.06及其以后的版本,将体系结构相关的内容合并,增加include文件夹,分离出通用库文件lib,其各 ...

  7. PHP标准库 (SPL) 笔记

    简介 SPL是Standard PHP Library(PHP标准库)的缩写. The Standard PHP Library (SPL) is a collection of interfaces ...

  8. php SPL学习

    数据结构 SplDoublyLinkedList - 该SplDoublyLinkedList类提供了一个双向链表的主要功能 SplStack - 该SplStack类提供了一种使用双向链表实现栈的主 ...

  9. PHP 标准库 SPL 之数据结构栈(SplStack)简单实践

    PHP 5.3.0 版本及以上的堆栈描述可以使用标准库 SPL 中的 SplStack class,SplStack 类继承双链表 ( SplDoublyLinkedList ) 实现栈. 代码: & ...

随机推荐

  1. 【做题】Codeforces Round #429 (Div. 2) E. On the Bench——组合问题+dp

    题目大意是给你n个数,求相邻两数相乘不是完全平方数的排列数. 一开始看到这题的时候,本人便想给相乘为完全平方数的数对建边,然后就写萎了... 后来通过集体智慧发现这个重要性质:对于自然数a,b,c,若 ...

  2. JPA原理与实践、多数据源配置

    参考博客: https://segmentfault.com/a/1190000015047290?utm_source=Weibo&utm_medium=shareLink&utm_ ...

  3. (zhuan) Attention in Neural Networks and How to Use It

    Adam Kosiorek About Attention in Neural Networks and How to Use It this blog comes from: http://akos ...

  4. [蓝桥] 历届试题 错误票据 (List用法,空格处理)

    时间限制:1.0s 内存限制:256.0MB 问题描述 某涉密单位下发了某种票据,并要在年终全部收回. 每张票据有唯一的ID号.全年所有票据的ID号是连续的,但ID的开始数码是随机选定的. 因为工作人 ...

  5. (转载)西门子PLC学习笔记十五-(数据块及数据访问方式)

    一.数据块 数据块是在S7 CPU的存储器中定义的,用户可以定义多了数据块,但是CPU对数据块数量及数据总量是有限制的. 数据块与临时数据不同,当逻辑块执行结束或数据块关闭,数据块中的数据是会保留住的 ...

  6. HDU 4301 Divide Chocolate(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4301 题意: 有一块n*2大小的巧克力,现在某人要将这巧克力分成k个部分,每个部分大小随意,问有多少种分法. 思 ...

  7. HDU 5792 World is Exploding(树状数组+离散化)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5792 题意: 思路: lmin[i]:表示左边比第i个数小的个数. lmax[i]:表示左边比第i个 ...

  8. Python day2_int以及string的常见方法1_笔记

    Python中,主要的基本类型有:数字(int型).字符串(string型).列表(list型).元祖(tuple型).字典(direct型).布尔值(boolean型) 1.int型 1.强转int ...

  9. SQL中的float类型的数据

    问题1.  如何在SQL中默认的使用float类型的数据 SQL中想要通过计算的方式最快的得到一个float类型的数据,只需要运算的其中一个值后面加上小数点就ok. 比如 :9/2=4 但是 :9/2 ...

  10. 学习笔记25—python基本运算法则

    1.矩阵的点乘: a*b, 矩阵乘法:dot(a*b),矩阵的次方:a**num (num = 2,表示2次)2.数组的并集,交集: >>> a = [1,2,3] >> ...