Page 1 of 1

I am stumped - please help!

Posted: Mon Sep 20, 2010 7:11 pm
by biz0r
I am stumped as to why this simple code will not work...anyone care to enlighten me?

Code: Select all

<?
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?

Re: I am stumped - please help!

Posted: Mon Sep 20, 2010 7:19 pm
by requinix
With variable variables you need the full path to the class name. So

Code: Select all

$x=__NAMESPACE__.'\\testClass';
And please use full open tags, not the short ones.

Re: I am stumped - please help!

Posted: Mon Sep 20, 2010 7:27 pm
by biz0r
That was it...thank you so much, I have been scratching my head over this one for the last 2 hours trying all sorts of craziness to get it to work properly.

Also, I do use the full open tags normally...I had just whipped up this test code for this error I was seeing. Thanks again!