问题
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