Class variable scope

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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Class variable scope

Post by Benjamin »

Any reason why this wouldn't work?

Code: Select all

class frustrated
{
    const MY_CONSTANT = 3;

    function __construct()
    {
        $test = "ya";
        $foo = foo::getInstance();
    }

    function test_vars()
    {
        echo MY_CONSTANT;
        echo $this->test;
        echo $this->foo;
    }
}
I am getting undefined variable notices for all 3 of the echo's.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: Class variable scope

Post by feyd »

Code: Select all

class frustrated
{
    const MY_CONSTANT = 3;

    function __construct()
    {
        $this->test = "ya";
        $this->foo = foo::getInstance();
    }

    function test_vars()
    {
        echo self::MY_CONSTANT;
        echo $this->test;
        echo $this->foo;
    }
}
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Ok I feel dumb now. I originally had it like that and was getting undefined variable notices, but it was because I had a $ in front of them.
Post Reply