biz0r Posted September 21, 2010 Share Posted September 21, 2010 I am stumped as to why this simple code will not work...anyone care to enlighten me? <? namespace test; class testClass { function go() { echo "hello world"; } } $x='testClass'; $t = new $x(); $t->go(); ?> Running that code gives me this error: Fatal error: Class 'testClass' not found in test.php on line 11 If I remove the namespace declaration it works fine...anyone? Quote Link to comment Share on other sites More sharing options...
biz0r Posted September 21, 2010 Author Share Posted September 21, 2010 Nevermind guys...I found out for variable variables you need to use the full path when referencing a class. The following fixed it for those interested: <?php namespace test; class testClass { function go() { echo "hello world"; } } $x = __NAMESPACE__.'\\testClass'; $t = new $x(); $t->go(); ?> Quote Link to comment Share on other sites More sharing options...
vungee Posted September 21, 2010 Share Posted September 21, 2010 You can declare the namespace using backslashes as well <? namespace test; class testClass { function go() { echo "hello world"; } } $x='\test\testClass'; $t = new $x(); $t->go(); ?> Quote Link to comment Share on other sites More sharing options...
biz0r Posted September 21, 2010 Author Share Posted September 21, 2010 yep...that is basically the same thing . Thanks for the reply. 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.