开发者

Differences in scope resolution and callbacks in PHP 5.3

Working on some code today, I found that the following would work in 5.3, but not earlier.

<?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 err开发者_如何转开发or: Cannot call method self::uasort_callback() or method does not exist

Just curious as to what this feature is called, and whether its considered good, bad, (or sloppy) practice, since changing it to

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

works fine in 5.2 as well.


Judging by the section on callbacks in the PHP manual, I'd say its called a "relative static class method call". See http://php.net/manual/en/language.pseudo-types.php(dead link)

// 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

Slightly different scenario, but I think the ability to call parent::who or self::uasort_callback are one in the same.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜