Gabriel Santos
Gabriel Santos

Reputation: 4974

Call namespace from variable

I need to make a dynamic namespace: [module]\controller\[controller]Controller, but the follow code does not work:

$namespace = '\Account\Controller\LoginController()';
new $namespace;

Like this:

new \Account\Controller\LoginController();

What I forgot?

Upvotes: 2

Views: 147

Answers (1)

gen_Eric
gen_Eric

Reputation: 227220

Don't add the () to the string. The () are used when calling the function, not in its name.

$namespace = '\Account\Controller\LoginController';
new $namespace;

If you want to pass params, add the () to the new call.

$namespace = '\Account\Controller\LoginController';
new $namespace('abc');

Upvotes: 2

Related Questions