<?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. .NET BackgroundWorker的一般使用方式

    代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...

  2. 再谈 iptables 防火墙的 指令配置

    手机上使用localhost为什么不能访问? 电脑上使用localhost 访问主页的原理是 电脑上有网站资源和服务器相关程序apache等的支持, 同时在 电脑的hosts文件中 有 127.0.0 ...

  3. e信与酸酸结合开wifi使用路由器上网

    关于e信"正常情况下"使用路由器网上是有方法的,入户线插上lan,电脑接lan拨号 我想要说的是连接e信后使用路由器上网,并且是绝对正常的思维 手机也是可以连接上wifi,但是手机 ...

  4. 【Dalston】【第一章】 服务治理(Eureka)

    Spring Cloud是一系列框架的集合,其基于Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,构建了服务治理(发现注册).配置中心.消息总线.负载均衡.断路器.数据监控.分 ...

  5. P2055 [ZJOI2009]假期的宿舍

    思路 看到复杂的匹配条件,发现要让一个人和一个床匹配,所以就每个有床的人(指本校学生)和t连一条边,每个需要床的人(指外校的人和不回家的人)和s连一条边,i和j互相认识就把i和j的床连在一起,自己和自 ...

  6. (转)Paper list of Meta Learning/ Learning to Learn/ One Shot Learning/ Lifelong Learning

    Meta Learning/ Learning to Learn/ One Shot Learning/ Lifelong Learning 2018-08-03 19:16:56 本文转自:http ...

  7. 【AI】微软人工智能学习笔记(三)

    微软R服务 01|开源的R R实际上是统计学的编程语言,主要作用是对数据挖掘,统计,分析,可视化,机器学习等. 02|微软R 03| HDInsight R Spark集群存储在azure blob ...

  8. Android四种布局方式

    线性布局 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap ...

  9. C#题目及答案(2)

    1. NET和C#有什么区别 答:.NET一般指 .NET FrameWork框架,它是一种平台,一种技术. C#是一种编程语言,可以基于.NET平台的应用. 2.一列数的规则如下: 1.1.2.3. ...

  10. Java 虚拟机 最易理解的 全面解析

    先上一个最容易理解的类实例化的内存模型案例截图: 转载自:https://www.zybuluo.com/Yano/note/321063 周志明著的<深入理解 Java 虚拟机>的干货~ ...