Jump to content

New Instance with namespace from string?


AbydosGater

Recommended Posts

I have a list of classes in a database that I wish to load and instanciate.

 

I know this can be done with something along the lines of:

$class = $row['name'];
$instance = new $class;

 

Ive tried the above syntax which works fine, but I am doing this with __autoload() and namespaces, so I tried the following syntax:

 

use Plugin as foo\bar\Plugins;

$class = $row['name'];
$instance = new Plugin\$class;

 

But this just throws an unexpected T_VARIABLE error.

 

Can anyone help me out with creating new instance of a class from a string with namespaces?

 

Thanks,

Andy

Link to comment
Share on other sites

You would need to use a string to reference the class with its namespace

 

    $class = "namespace\of\the\\$class";

 

Also note that you won't be able to use the aliased name (Plugin\$class) like you want to, the full namespace path must be used (whether you like it or not  :-[ ).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.