Differences in area resolution and callbacks in PHP 5.3

While working on some code today, I found that the following will work in 5.3, but not before.

<?php

class Test{
    public function uasort(){
        $array = array( 'foo' => 'bar', 123 => 456 );
        uasort( $array, 'self::uasort_callback' );

        return $array;
    }

    static private function uasort_callback( $a, $b ){
        return 1;
    }
}

$Test = new Test;
var_dump( $Test->uasort() );

// version 5.3.2  - works fine
// version 5.2.13 - Fatal error: Cannot call method self::uasort_callback() or method does not exist

It’s just curious what this function is called and whether it considers itself a good, bad (or sloppy) practice, changing it to

uasort( $array, 'Test::uasort_callback' );

works fine in 5.2 as well.

+3
source share
1 answer

Judging by the callbacks section in the PHP manual, I would say that it is called a “relative call to a static class method”. See http://php.net/manual/en/language.pseudo-types.php

// Type 4: Static class method call (As of PHP 5.2.3)
call_user_func('MyClass::myCallbackMethod');

// Type 5: Relative static class method call (As of PHP 5.3.0)
class A {
    public static function who() {
        echo "A\n";
    }
}

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

call_user_func(array('B', 'parent::who')); // A

A slightly different scenario, but I think the ability to call is parent::whoeither self::uasort_callbackthe same.

+2
source

Source: https://habr.com/ru/post/1792117/


All Articles