通过引用处理__get() [英] Working with __get() by reference
本文介绍了通过引用处理__get()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用一个如下所示的示例类:
class Test {
元素是否有针对性?
public function& __ get($ name ){
print_r($ name);
$ b
Test的一个实例
会反弹输出:
$ myTest = new Test;
$ myTest-> foo ['bar'] ['hello'] ='world';
//输出只有foo
有没有办法获得更多关于什么维度的信息的数组正在被访问,向我展示(来自前面的例子)
foo
的bar
元素,以及hello
bar
解决方案你不能用当前的实现。为了达到这个目的,你必须创建一个数组对象(即:一个实现
ArrayAccess
的对象)。例如:
class SuperArray实现ArrayAccess {
protected $ _data = array();
protected $ _parents = array();
public function __construct(array $ data,array $ parents = array()){
$ this-> _parents = $ parents;
foreach($ data as $ key => $ value){
if(is_array($ value)){
$ value = new SuperArray($ value,array_merge($ this-> ; _parents,array($ key)));
}
$ this [$ key] = $ value;
public function offsetGet($ offset){
if(!empty($ this-> _parents))echo['.implode ('] [',$ this-> _parents)。'];
echo['$ offset']正在被访问......;
返回$ this-> _data [$ offset];
}
public function offsetset($ offset,$ value){
if($ offset ==='')$ this-> _data [] = $ value;
else $ this-> _data [$ offset] = $ value;
}
public function offsetUnset($ offset){
unset($ this-> _data [$ offset]);
}
公共函数offsetExists($ offset){
return isset($ this-> _data [$ offset]);
}
}
class Test {
protected $ foo;
public function __construct(){
$ array ['bar'] ['hello'] ='world';
$ this-> foo = new SuperArray($ array);
}
公共函数__get($ name){
echo $ name。'正在被访问。'。PHP_EOL;
返回$ this-> $ name;
}
}
$ test = new Test;
echo $ test-> foo ['bar'] ['hello'];
应输出:
正在访问foo。正在访问
['bar']
['bar'] ['hello']正被访问
world
With an example class such as this:
class Test{ public function &__get($name){ print_r($name); } }
An instance of
Test
will kick back output as such:$myTest = new Test; $myTest->foo['bar']['hello'] = 'world'; //outputs only foo
Is there a way I can get more information about what dimension of the array is being accessed, showing me (from the previous example) that the
bar
element offoo
, and thehello
element ofbar
are being targeted?解决方案You can't with the current implementation. In order for this to work, you will have to create an array object (i.e.: an object that implements
ArrayAccess
). Something like:class SuperArray implements ArrayAccess { protected $_data = array(); protected $_parents = array(); public function __construct(array $data, array $parents = array()) { $this->_parents = $parents; foreach ($data as $key => $value) { if (is_array($value)) { $value = new SuperArray($value, array_merge($this->_parents, array($key))); } $this[$key] = $value; } } public function offsetGet($offset) { if (!empty($this->_parents)) echo "['".implode("']['", $this->_parents)."']"; echo "['$offset'] is being accessed\n"; return $this->_data[$offset]; } public function offsetSet($offset, $value) { if ($offset === '') $this->_data[] = $value; else $this->_data[$offset] = $value; } public function offsetUnset($offset) { unset($this->_data[$offset]); } public function offsetExists($offset) { return isset($this->_data[$offset]); } } class Test{ protected $foo; public function __construct() { $array['bar']['hello'] = 'world'; $this->foo = new SuperArray($array); } public function __get($name){ echo $name.' is being accessed.'.PHP_EOL; return $this->$name; } } $test = new Test; echo $test->foo['bar']['hello'];
Should output:
foo is being accessed. ['bar'] is being accessed ['bar']['hello'] is being accessed world
这篇关于通过引用处理__get()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文