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 Link to comment https://forums.phpfreaks.com/topic/216252-new-instance-with-namespace-from-string/ 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 ). Link to comment https://forums.phpfreaks.com/topic/216252-new-instance-with-namespace-from-string/#findComment-1123825 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 Link to comment https://forums.phpfreaks.com/topic/216252-new-instance-with-namespace-from-string/#findComment-1123931 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. Link to comment https://forums.phpfreaks.com/topic/216252-new-instance-with-namespace-from-string/#findComment-1123987 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.