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 error: 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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.