Page 1 of 1

Class variable scope

Posted: Sat Aug 19, 2006 1:07 pm
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.

Re: Class variable scope

Posted: Sat Aug 19, 2006 1:11 pm
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;
    }
}

Posted: Sat Aug 19, 2006 1:16 pm
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.