6

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
  • You can't have dynamic namepaces: why not create a language factory instead? Commented May 24, 2016 at 16:24

1 Answer 1

9

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yep, I am familiar with that solution: of creating classes from dynamic class names. However, the best solution for me would have been the dynamic namespaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.