Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP dynamic namespaces

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"?

like image 974
Catalin Avatar asked Mar 13 '23 05:03

Catalin


1 Answers

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

like image 69
ShiraNai7 Avatar answered Mar 24 '23 04:03

ShiraNai7