Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forwarding and non-forwarding calls in late static binding (PHP 5.3)

Tags:

php-5.3

<?php
class A {
    public static function foo() {
        static::who();
    }

    public static function who() {
        echo __CLASS__."\n";
    }
}

class B extends A {
    public static function test() {
        A::foo();
        parent::foo();
        self::foo();
    }

    public static function who() {
        echo __CLASS__."\n";
    }
}
class C extends B {
    public static function who() {
        echo __CLASS__."\n";
    }
}

C::test();
?> 

given below is the output:

A
C
C

can anyone evaluate tell how that output has been produced?

like image 333
Poonam Bhatt Avatar asked Feb 20 '23 03:02

Poonam Bhatt


1 Answers

The result from the first line in test() "A" does not leverage Late Static Binding, since you are, in all cases, directly calling the class "A"'s implementation of foo (technically it is LSB, but static is bound to A). The second and third lines demonstrate expected behavior with the static keyword, the chain of inheritance does not matter, static refers to the called class. So even though you are calling parent::foo() from B, that implementation used LSB where static keyword resolves to the called class, which is C. The same happens with self::foo().

like image 91
Chris Trahey Avatar answered Apr 07 '23 03:04

Chris Trahey