【发布时间】:2012-08-15 12:42:09
【问题描述】:
有没有办法覆盖同一属性上 __get 和 __set 的递归限制。我希望能够以不同于第一个条目的方式处理第二个重新进入。
这个代码示例并不实用,但最能说明问题。
class Foo {
public function __set($name,$value){
print "$name entered\n";
this->$name = $value; // want it to recurse here
}
}
$a = new Foo();
$a->baz = "derp";
print $a->baz;
// should get (cannot test at the moment)
// baz entered
// derp <- you get derp because the current php implementation creates an instance variable from the second call to __set
我的网络中断了,所以我在手机上打字,所以很可能有错别字。
【问题讨论】:
-
您能提供一个代码示例吗?
-
对不起,我不知道你的意思。这样 __set 就不会递归(顺便说一句,我认为您的意思是
this->$var = $args;)。 -
你的权利,它不是递归的,这就是问题所在。 Php 阻止它重新使用并创建一个实例变量。我想知道是否有办法放宽对 __set() 和 __get() 的递归限制。
-
请参阅stackoverflow.com/questions/11980996/…,因为这个问题与那个问题有关,因为我试图找到一个比制作 _set/_get 方法更优雅的解决方案。
-
我不明白您要解决的问题。您谈论递归,但没有递归,您说您尝试找到比 __get() 和 __set() 魔术方法更优雅的解决方案,但是您确实使用了它们。请在完整的终端前编辑您的问题,正确解释问题所在。
标签: php php-internals