PHP dynamic namespaces

淺唱寂寞╮ 提交于 2020-05-13 05:26:14

问题


I need to be able to do this:

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

Obviously this won't work. So how can I do this? Have "dynamic namespaces"?


回答1:


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



来源:https://stackoverflow.com/questions/37418958/php-dynamic-namespaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!