$this 不在 PHP 5.3.4 中的 user_call_func_array() 中传播

发布于 2024-10-27 19:53:42 字数 1426 浏览 2 评论 0原文

我正在尝试创建一个可以在实例化后通过其他类扩展的对象。我似乎发现了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

記柔刀 2024-11-03 19:53:42

如果在第二次调用中未设置 $this ,这是很自然的

$this 表示类的当前对象,但在您的情况下,类本身正在调用该方法,因此 $未设置此

class::method(); //The class itself is calling the method hence $this will be unset

Object->method();//$this will be set in this case because object is calling the method 

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.

class::method(); //The class itself is calling the method hence $this will be unset

Object->method();//$this will be set in this case because object is calling the method 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文