Relative namespaces and call_user_func()

前端 未结 2 373
猫巷女王i
猫巷女王i 2020-12-20 18:45

Code speaks better than words:

namespaces.php:



        
相关标签:
2条回答
  • 2020-12-20 19:43

    You have to use the fully qualified classname in callbacks.

    See Example #3 call_user_func() using namespace name

    <?php
    
    namespace Foobar;
    
    class Foo {
        static public function test() {
            print "Hello world!\n";
        }
    }
    
    call_user_func(__NAMESPACE__ .'\Foo::test'); // As of PHP 5.3.0
    call_user_func(array(__NAMESPACE__ .'\Foo', 'test')); // As of PHP 5.3.0
    

    I believe this is because call_user_func is a function from the global scope, executing the callback from the global scope as well. In any case, see first sentence.

    Also see the note aboveExample #2 Dynamically accessing namespaced elements which states

    One must use the fully qualified name (class name with namespace prefix).

    0 讨论(0)
  • 2020-12-20 19:45

    In current versions of PHP, the way you have it is the way it is -- when using a string to reference a classname, it needs to be fully qualified with it's complete namespace. It's not great, but that's the way it is.

    In the forthcoming PHP v5.5, they will include a feature to address this, by providing a new Classname::class syntax, which you can use instead of putting the FQN classname in a string.

    For more info on this, please see the relevant PHP RFC page here: https://wiki.php.net/rfc/class_name_scalars

    Your code would look something like this:

    return call_user_func([models\$name::class,"getInstance"]);
    

    That may not be exact; I don't have a copy of 5.5 to test with to confirm. But either way, the new syntax will make things a lot better for use cases like yours.

    0 讨论(0)
提交回复
热议问题