arrayAccess

   ArrayAccess 接口的作用是提供像访问数组一样访问对象的能力。这只是一种写法。

class Configuration implements ArrayAccess
{
private $configArray;

public function getA()
{
return "getAMthod";
}
//获取值 mixed $config['Binzy'];
function offsetGet($index)
{
return $this->$index(); //数组访问的时候就会调用相应的方法,具体方法里面怎么实现,是自己业务决定的。
// return $this->configArray[$index];
}

//void 设置值 $config['Binzy'] = '1222'
function offsetSet($index, $newvalue)
{
$this->configArray[$index] = $newvalue;
}

// boolean 检查一个偏移位置是否存在 boolean empty($config["asd"]) isset($config["asd"]) 的时候才会调用
function offsetExists($index)
{
return isset($this->configArray[$index]);
}

//void 调用unset("xxx")的时候才会调用
function offsetUnset($index)
{
unset($this->configArray[$index]);
}

}

$a = new Configuration();
//$a["a"] = "1";
//echo $a['a'];
echo $a["getA"];
原文地址:https://www.cnblogs.com/lengthuo/p/7228755.html