Instantiating class by string in current namespace

前端 未结 1 2082
别那么骄傲
别那么骄傲 2021-02-09 00:38

I\'m trying to instantiate an object of a dynamically created classname. I\'m using namespaces and the class I want to instantiate is in the same namespace.

To examplify

相关标签:
1条回答
  • 2021-02-09 01:16

    When you use a string with new you need to provide a fully qualified class name.

    You can do this with __NAMESPACE__:

    $fullclass = __NAMESPACE__ . '\\' . $class;
    new $fullclass;
    

    See the documentation for the new operator and the __NAMESPACE__ magic constant.

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