<?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. 2018 蓝桥杯省赛 B 组模拟赛(五)

    A模拟 代码1 #include<bits/stdc++.h> using namespace std; int n = 101; int a[120][120]; int ans = 0 ...

  2. hihoCoder week10 后序遍历

    题目链接 https://hihocoder.com/contest/hiho10/problem/1 给出先序  中序 求 后序 #include <bits/stdc++.h> usi ...

  3. P2617 Dynamic Rankings(带修主席树)

    所谓带修主席树,就是用树状数组的方法维护主席树的前缀和 思路 带修主席树的板子 注意数据范围显然要离散化即可 代码 #include <cstdio> #include <cstri ...

  4. Docker之Swarm

    Docker学习笔记 — Swarm搭建Docker集群 Swarm在schedule节点运行容器的时候,会根据指定的策略来计算最适合运行容器的节点,目前支持的策略有:spread, binpack, ...

  5. NOI1999 生日蛋糕

    #include<iostream> #include<cstdio> #include<cmath> using namespace std; #define I ...

  6. ECharts 使用总结

    1.去掉Echarts 图标上边框和右边框 option = { title: { text: '未来一周气温变化', subtext: '纯属虚构' }, grid: { show: 'true', ...

  7. 在js文件里调用另一个js文件里的函数

    这个是我今天解决的一个小问题,我在创建界面的时候,根据不同的界面需求对应创建了不同的js文件来搭建界面,搭建完毕之后再将各个生成页面的函数汇总到主界面上,通过visibility属性切换显示,这时候出 ...

  8. nginx的使用教程

    一.基本概念 1.1 正向代理和反向代理 (参考文档:https://www.cnblogs.com/hafiz/p/7233306.html) 假设我们给定客户端A.代理服务器B.以及最终服务器C ...

  9. 小程序模板template

    WXML提供模板(template),可以在模板中定义代码片段,然后在不同的地方调用. 定义模板 使用 name 属性,作为模板的名字.然后在<template/>内定义代码片段,如: & ...

  10. cacheManager ABP中的缓存

    ABP的缓存是key---(key,value) 形式存储 GetCache获取到的是ICache类型   如果知道这个ICache的具体类型  可以直接强转Icache.AsTyped<int ...