<?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);

最新文章

  1. shell简单用法笔记(shell中数值运算)二
  2. Lind.DDD.ExpressionExtensions动态构建表达式树,实现对数据集的权限控制
  3. EasyUI实现工地领款单项目
  4. 学习笔记:HSB、HSL
  5. JAVA 23种设计模式(转)
  6. Hbase基础操作
  7. Heritrix源码分析(六) Heritrix的文件结构分析(转)
  8. 【风马一族_Android】造作app的效果图
  9. Linux SocketCan client server demo hacking
  10. SharePoint 学习记事(一)
  11. crossfire 346# B
  12. Python一路走来 面向对象1
  13. 【转载】"library not found for - "解决办法
  14. click延时300ms的故事
  15. MySQL ID排序乱了的解决办法
  16. 搭建vue环境
  17. Win10系列:WinJS库控件
  18. Guarding the Chessboard(UVa 11214)
  19. [日常] Go语言圣经-指针对象的方法-bit数组习题
  20. 探索Java8:(二)Function接口的使用

热门文章

  1. centos设置中文输入法无效的解决办法
  2. 【做题】51NOD1518 稳定多米诺覆盖——容斥&amp;dp
  3. Spring核心简介
  4. C# 禁止任务管理器关闭
  5. 题解——HDU 2089 不要62(数位DP)
  6. 第一章(欢迎进入node.js世界)
  7. 使用路由传参时,query与params的区别!
  8. CommandLine exe参数
  9. Use Memory Layout from Target Dialog Scatter File
  10. HDU 5459 Jesus Is Here(递推)