$this 不在 PHP 5.3.4 中的 user_call_func_array() 中传播
我正在尝试创建一个可以在实例化后通过其他类扩展的对象。我似乎发现了 user_call_func_array
(以及这个函数系列)没有正确传播 $this
变量的问题。
请考虑以下事项:
// Base class
class baseClass {
public $some_value = 'foobar';
public function callManually() {
extensionClass::extendedMethod('hello');
}
public function callDynamically($class,$method) {
call_user_func_array("$class::$method",array('hello'));
}
}
// Extension class
class extensionClass {
public function extendedMethod($local_value) {
if(isset($this)) {
echo '$this is set. Local value = '.$local_value.'. Base value = '.$this->some_value."\n";
} else {
echo '$this is not set. Boo!'."\n";
}
}
}
// Create the base object and call extended method
$base_class = new baseClass;
$base_class->callManually();
$base_class->callDynamically('extensionClass','extendedMethod');
callManually()
和 callDynamically()
都在扩展类中调用 extendedMethod()
。因此,人们期望脚本生成以下内容:
$this is set. Local value = hello. Base value = foobar $this is set. Local value = hello. Base value = foobar
但是,由于 user_call_func_array
未正确传播 $this
,我在 PHP 5.3.4 中得到以下内容Mac OS X:
$this is set. Local value = hello. Base value = foobar $this is not set. Boo!
任何人都可以阐明这一点或为我的问题提供替代解决方案吗?
谢谢。
I'm attempting to create an object which can be extended by additional classes after instantiation. I seem to have found a problem with user_call_func_array
(and this family of functions) not propagating the $this
variable properly.
Please consider the following:
// Base class
class baseClass {
public $some_value = 'foobar';
public function callManually() {
extensionClass::extendedMethod('hello');
}
public function callDynamically($class,$method) {
call_user_func_array("$class::$method",array('hello'));
}
}
// Extension class
class extensionClass {
public function extendedMethod($local_value) {
if(isset($this)) {
echo '$this is set. Local value = '.$local_value.'. Base value = '.$this->some_value."\n";
} else {
echo '$this is not set. Boo!'."\n";
}
}
}
// Create the base object and call extended method
$base_class = new baseClass;
$base_class->callManually();
$base_class->callDynamically('extensionClass','extendedMethod');
Both callManually()
and callDynamically()
invoke extendedMethod()
within the extension class. Therefore, one would expect the script to produce the following:
$this is set. Local value = hello. Base value = foobar $this is set. Local value = hello. Base value = foobar
However, because user_call_func_array
isn't propagating $this
correctly, I'm getting the following in PHP 5.3.4 on Mac OS X:
$this is set. Local value = hello. Base value = foobar $this is not set. Boo!
Can anyone shed some light on this or offer an alternative solution to my problem?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果在第二次调用中未设置
$this
,这是很自然的$this
表示类的当前对象,但在您的情况下,类本身正在调用该方法,因此$未设置此
。It is natural if
$this
is not set in second call$this
denotes the current object of class but in your case the class itself is calling the method hence$this
is not set.