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 ) 实现栈. 代码: & ...
随机推荐
- git 如何revert指定范围内的commit并且只生成一个新的commit?
答:一共分成两步 一. revert多个commit并生成多个新的commit git revert <old commit>^..<new commit> 二. 使用reba ...
- P3833 [SHOI2012]魔法树
思路 树剖板子 注意给出点的编号是从零开始的 代码 #include <cstdio> #include <algorithm> #include <cstring> ...
- ffmpeg 下载安装和简单应用
一.ffmpeg下载 先到http://ffmpeg.org/下载ffmpeg安装文件 二.ffmpeg安装 1.解压下载完的ffmpeg-20190319-f8075b2-win64-shared. ...
- FI 业务
f-02 post f-03 clear[account]-> f-04 post with clear fb70/f-22 f-32 clear[account]->f-28 post ...
- 51nod 1615 跳跃的杰克
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1615 题意: 思路:一开始是觉得一旦超过了终点,中间某个地方往相反地方跳 ...
- SPOJ 694 Distinct Substrings(不相同子串个数)
https://vjudge.net/problem/SPOJ-DISUBSTR 题意: 给定一个字符串,求不相同的子串的个数. 思路: #include<iostream> #inclu ...
- SpringLog4j日志体系实现方式
1.通过web.xml读取log4j配置文件内容 2.通过不同的配置信息,来实现不同的业务输出,注意:log4j可以写入tomcat容器,也可以写入缓存,通过第三方平台读取 #输入规则#log4j.r ...
- 转 lightmap
小记一下用法与问题,时更 surface shader就不用操心了,自带lightmap计算 主要是vertex fragment shader部分 Unity5 bake light map有三种情 ...
- go 接口以及对象传递
// Sample program to show how to use an interface in Go. package main import ( "fmt" ) // ...
- CentOS6.5下安装配置MySQL数据库
一.MySQL简介 说到数据库,我们大多想到的是关系型数据库,比如MySQL.Oracle.SQLServer等等,这些数据库软件在Windows上安装都非常的方便,在Linux上如果要安装数据库,咱 ...