Andikac
Andikac

Reputation: 434

late static binding failed on magic method

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();

outputs
string 'parents' (length=7)
string 'child' (length=5)
string 'child' (length=5)
string 'child' (length=5)

expected outputs
string 'parents' (length=7)
string 'parents' (length=5)
string 'child' (length=5)
string 'child' (length=5)

it seems that late static binding fail on calling magic method from parrent class, or i miss something ?

thanks :D

Upvotes: 0

Views: 42

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385174

This is entirely by design.

parent::call() invokes parent::call(). From the documentation:

More precisely, late static bindings work by storing the class named in the last "non-forwarding call". In case of static method calls, this is the class explicitly named (usually the one on the left of the :: operator); in case of non static method calls, it is the class of the object. A "forwarding call" is a static one that is introduced by self::, parent::, static::, or, if going up in the class hierarchy, forward_static_call().

Your call uses parent::, so there is no way you can "use" late static bindings here.

$this->call() is the correct way to achieve polymorphism in this case.

Upvotes: 1

Related Questions