1. <?php
  2.  
  3. /**
  4. * Class MyArrayAccess
  5. * 提供像访问数组一样访问对象的能力的接口
  6. */
  7. class MyArrayAccess implements ArrayAccess
  8. {
  9. private $container;
  10.  
  11. public function __construct(Array $arr)
  12. {
  13. $this->container = $arr;
  14. }
  15.  
  16. // 某键是否存在 返回布尔值
  17. public function offsetExists($offset)
  18. {
  19. var_dump(__METHOD__);
  20. return isset($this->container[$offset]);
  21. }
  22.  
  23. // 获取键对应的值 返回值mixed
  24. public function offsetGet($offset)
  25. {
  26. var_dump(__METHOD__);
  27. return isset($this->container[$offset]) ? $this->container[$offset] : null;
  28. }
  29.  
  30. // 赋值
  31. public function offsetSet($offset, $value)
  32. {
  33. if (is_null($offset)) {
  34. $this->container[] = $value;
  35. } else {
  36. $this->container[$offset] = $value;
  37. }
  38. var_dump(__METHOD__);
  39. }
  40.  
  41. // 删除键值
  42. public function offsetUnset($offset)
  43. {
  44. unset($this->container[$offset]);
  45. var_dump(__METHOD__);
  46. }
  47.  
  48. }
  49.  
  50. 运行:
  1. string(27) "MyArrayAccess::offsetExists"
  2. bool(true)
  3. string(24) "MyArrayAccess::offsetGet"
  4. int(2)
  5. string(26) "MyArrayAccess::offsetUnset"
  6. string(27) "MyArrayAccess::offsetExists"
  7. bool(false)
  8. string(24) "MyArrayAccess::offsetSet"
  9. string(24) "MyArrayAccess::offsetGet"
  10. string(7) "A value"
  11. string(24) "MyArrayAccess::offsetGet"
  12. A valuestring(24) "MyArrayAccess::offsetSet"
  13. string(24) "MyArrayAccess::offsetSet"
  14. string(24) "MyArrayAccess::offsetSet"
  15. object(MyArrayAccess)#1 (1) {
  16. ["container":"MyArrayAccess":private]=>
  17. array(6) {
  18. ["one"]=>
  19. int(1)
  20. ["three"]=>
  21. int(3)
  22. ["two"]=>
  23. string(7) "A value"
  24. [0]=>
  25. string(8) "Append 1"
  26. [1]=>
  27. string(8) "Append 2"
  28. [2]=>
  29. string(8) "Append 3"
  30. }
  31. }
  1.  
  1. <?php
  2.  
  3. /**
  4. * 单例
    * 四私一公
  5. */
  6. trait Singleton
  7. {
  8. static private $instance;
  9.  
  10. private function __construct()
  11. {
  12.  
  13. }
  14.  
  15. private function __clone()
  16. {
  17.  
  18. }
  19.  
  20. private function __wakeup()
  21. {
  22.  
  23. }
  24.  
  25. static public function getInstance()
  26. {
  27. if (is_null(static::$instance)) {
  28. $class = new ReflectionClass(get_called_class());
  29. static::$instance = $class->newInstanceWithoutConstructor();
  30. $method = $class->getConstructor();
  31. $method->setAccessible(true);
  32. $method->invokeArgs(static::$instance, func_get_args());
  33. }
  34. return static::$instance;
  35. }
  36. }
  37.  
  38. /**
  39. * 对象当数组用
  40. */
  41. class myAccess implements ArrayAccess
  42. {
  43. use Singleton;
  44.  
  45. private $dataSource = [];
  46.  
  47. /**
  48. * 可返回任意类型
  49. */
  50. public function offsetGet($offset)
  51. {
  52. var_dump(__METHOD__);
  53. return $this->dataSource[$offset] ?? null;
  54. }
  55.  
  56. /**
  57. * 无返回值
  58. */
  59. public function offsetSet($offset, $value)
  60. {
  61. var_dump(__METHOD__);
  62. $this->dataSource[$offset] = $value;
  63. }
  64.  
  65. /**
  66. * 返回布尔值
  67. * 如果用empty()检测且返回值为true,则自动调用offsetGet
  68. */
  69. public function offsetExists($offset)
  70. {
  71. var_dump(__METHOD__);
  72. return isset($this->dataSource[$offset]);
  73. }
  74.  
  75. /**
  76. * 无返回值
  77. */
  78. public function offsetUnset($offset)
  79. {
  80. var_dump(__METHOD__);
  81. unset($this->dataSource[$offset]);
  82. }
  83. }
  84.  
  85. // use
  86. echo '<pre>';
  87.  
  88. $obj = myAccess::getInstance();
  89. $obj['name'] = 'tom';
  90. isset($obj['name']);
  91. empty($obj['name']);
  92. unset($obj['name']);
  93. 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. git 如何revert指定范围内的commit并且只生成一个新的commit?

    答:一共分成两步 一. revert多个commit并生成多个新的commit git revert <old commit>^..<new commit> 二. 使用reba ...

  2. P3833 [SHOI2012]魔法树

    思路 树剖板子 注意给出点的编号是从零开始的 代码 #include <cstdio> #include <algorithm> #include <cstring> ...

  3. ffmpeg 下载安装和简单应用

    一.ffmpeg下载 先到http://ffmpeg.org/下载ffmpeg安装文件 二.ffmpeg安装 1.解压下载完的ffmpeg-20190319-f8075b2-win64-shared. ...

  4. FI 业务

    f-02 post f-03 clear[account]-> f-04 post with clear fb70/f-22 f-32 clear[account]->f-28 post ...

  5. 51nod 1615 跳跃的杰克

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1615 题意: 思路:一开始是觉得一旦超过了终点,中间某个地方往相反地方跳 ...

  6. SPOJ 694 Distinct Substrings(不相同子串个数)

    https://vjudge.net/problem/SPOJ-DISUBSTR 题意: 给定一个字符串,求不相同的子串的个数. 思路: #include<iostream> #inclu ...

  7. SpringLog4j日志体系实现方式

    1.通过web.xml读取log4j配置文件内容 2.通过不同的配置信息,来实现不同的业务输出,注意:log4j可以写入tomcat容器,也可以写入缓存,通过第三方平台读取 #输入规则#log4j.r ...

  8. 转 lightmap

    小记一下用法与问题,时更 surface shader就不用操心了,自带lightmap计算 主要是vertex fragment shader部分 Unity5 bake light map有三种情 ...

  9. go 接口以及对象传递

    // Sample program to show how to use an interface in Go. package main import ( "fmt" ) // ...

  10. CentOS6.5下安装配置MySQL数据库

    一.MySQL简介 说到数据库,我们大多想到的是关系型数据库,比如MySQL.Oracle.SQLServer等等,这些数据库软件在Windows上安装都非常的方便,在Linux上如果要安装数据库,咱 ...