AbydosGater Posted October 19, 2010 Share Posted October 19, 2010 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 Quote Link to comment Share on other sites More sharing options...
salathe Posted October 19, 2010 Share Posted October 19, 2010 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 ). Quote Link to comment Share on other sites More sharing options...
AbydosGater Posted October 19, 2010 Author Share Posted October 19, 2010 This worked perfectly. I do not mind having to use the full namespace path as long as the namespace works Thank you very much salathe for such a quick reply. Andy Quote Link to comment Share on other sites More sharing options...
salathe Posted October 19, 2010 Share Posted October 19, 2010 No problemo, I'm happy to help folks who show willing to learn. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.