魔术方法的后期静态绑定失败

时间:2014-05-25 12:14:39

标签: php

class parents {

public function __call( $name , $arguments )    {
    var_dump( __CLASS__ );
}

public function test () {
    var_dump( __CLASS__ );
}
}



class child extends parents{

public function __call( $name , $arguments )    {
    var_dump( __CLASS__ );
}

public function test () {
    var_dump( __CLASS__ );
}

public function lateStaticTest ()   {
    parent::test();
    parent::call();

    $this->test();
    $this->call();
}

}


$child = new child();
$child->lateStaticTest();

输出
字符串'父母'(长度= 7)
字符串'child'(长度= 5)
字符串'child'(长度= 5)
字符串'child'(长度= 5)

预期产出
字符串'父母'(长度= 7)
字符串'父母'(长度= 5)
字符串'child'(长度= 5)
字符串'child'(长度= 5)

似乎后期静态绑定在从parrent类调用magic方法时失败了,或者我想念一些东西?

谢谢:D

1 个答案:

答案 0 :(得分:1)

这完全是设计的。

parent::call()调用parent::call()。来自the documentation

  

更确切地说,后期静态绑定通过存储在最后一次非转发呼叫" 中命名的类来工作。在静态方法调用的情况下,这是明确命名的类(通常是::运算符左侧的类);在非静态方法调用的情况下,它是对象的类。 A"转发电话"是由self::parent::static::引入的静态版本,或者,如果在类层次结构中上升,则为forward_static_call()

您的通话使用parent::,因此您无法使用"这里的后期静态绑定。

在这种情况下,

$this->call()是实现多态的正确方法。