I can’t find the reason in the documentation why this code works fine in 7.0.5:

<?php class A { private static $a; function __construct() { self::$a = new B; echo "1. ".__CLASS__.' '.self::$a->b."\n"; self::$a->b = 'A::test'; //7.0.5 ok echo "2. ".__CLASS__.' '.self::$a->b."\n"; echo self::$a; // ok expected } public static function test() { return 'ok'; } } class B { public $b; public function __toString() { echo "3. ".__CLASS__.' '.$this->b."\n"; $tmp = $this->b; echo "4. ".__CLASS__.' '.$tmp.' '.is_string($this->b).' '.is_callable($this->b)."\n"; $res = $tmp(); echo "5. ".__CLASS__.' '.$res."\n"; return $res; } } $a = new A; 

Result:

 1. A 2. AA::test 3. BA::test 4. BA::test 1 1 5. B ok ok 

And it gives an error for 5.6.2 "Call to undefined function A :: test ()" on the line $ res = $ tmp ()?

Result:

 1. A 2. AA::test 3. BA::test 4. BA::test 1 1 Fatal error: Call to undefined function A::test() 

If you use call_user_func instead of $ res = $ tmp () - everything works, but I really want to access the function through a variable. Maybe? I ask for help and knowledge.

    1 answer 1

    if my memory serves me, then until version 7 instead

     self::$a->b = 'A::test'; //7.0.5 ok 

    need to

     self::$a->b = array('A', 'test'); //5.6 should work I guess... 
    • Yes, you are right, it will work that way. It is strange that the direct call A :: test () and call_user_func normally work, but a call through a variable is not. It seems that this is a feature of implementation. The documentation came across in the example: $ func = "Foo :: bar"; $ func (); // displays "bar" in PHP 7.0.0 and higher; in previous versions, this will lead to a fatal error - raciasolvo
    • Let's wait, maybe there is some trick to bypass this limitation. - raciasolvo
    • and here I am, who did not understand, why do you need "A :: test"? What prevents to develop taking into account restrictions? - AlexandrX
    • ... especially since array ('A', 'test') in version 7 also works, which means it can be used as a cross-version approach (although, I confess, I did not check, but I am 90% sure). - AlexandrX
    • No one bothers to develop a limited view. It is clear that you can do the conversion. In the task string type was important. Transformations are not very desirable, but, of course, if they are not possible, they will be used. - raciasolvo