PHP dynamic namespaces

前端 未结 1 1445
被撕碎了的回忆
被撕碎了的回忆 2021-01-13 18:34

I need to be able to do this:

$ns = \"\\\\common\\\\components\\\\cfoBi\\\\i18n\\\\{$countryCode}\\\\gimmea\";
use $USP;

Obviously this won

相关标签:
1条回答
  • 2021-01-13 19:06

    Not possible. Namespaces, imports and aliases are resolved at compile time.

    However, it is possible to create objects from a class name that is built at runtime:

    $className = "common\\components\\cfoBi\\i18n\\{$countryCode}\\gimmea";
    
    $object = new $className();
    

    See PHP docs: http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.new

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