I am stumped - please help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
biz0r
Forum Newbie
Posts: 13
Joined: Mon Oct 27, 2003 3:21 pm
Location: Houston, TX

I am stumped - please help!

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: I am stumped - please help!

Post 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.
biz0r
Forum Newbie
Posts: 13
Joined: Mon Oct 27, 2003 3:21 pm
Location: Houston, TX

Re: I am stumped - please help!

Post 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!
Post Reply