0

Is it possible to use a namespace in PHP from a variable. I want to load classes dynamically based on a configuration array; something like that:

foreach ($content_types as $key => $content_type) { $namespace = $content_type['entity']; use $namespace; } 

Instead doing this:

use Bundle\Entity\User; use Bundle\Entity\Project; 

Note: I am using Symfony, the error message returned when I do that is:

Parse Error: syntax error, unexpected 'use' (T_USE)

1 Answer 1

4

Im not sure if I get you right, but you want probably just this

foreach ($content_types as $key => $content_type) { $class = $content_type['entity']; $entity = new $class(); // do something with $entity.. } 
Sign up to request clarification or add additional context in comments.

3 Comments

OP doesnt want instantiate, but requiring them
You don't have to "require" them, if you dont use them, and if you already know the fqcn, you could just work with them
Yeah it worked! Apparently I was mixing namespace and class concepts. I didn't know I could just initiate a new object this way. Thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.