Re: Object reference...
| From: | Matt McClanahan | Date: | Sun, 21 May 2000 21:18:18 +0000 |
| Subject: | Re: Object reference... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-260@lists.php.net to get a copy of this message | ||
On Sun, 21 May 2000, David Tiselius wrote:
> I can't get this to work inside a class:
>
> $object->foo = "something";
> $bar = "foo";
> print $object->$bar;
>
> I thought I'd get something out...
> Outside a class the above works. Everything concerned is declared within the
> same function in the class so i shouldn't have to refer to my object with
> $this->object, right? (anyway, I've tried it and I still get nothing).
> print $object->foo; works so the object's not empty.
Actually, when you're inside a class method you do need to use $this-> to
refer to class variables. If you don't, it's assumed you're referring to
a variable local to that method.
That is, when you say $bar = "foo"; inside a class method, that doesn't
automatically create a class variable. So if we have
class Foobar
{
var $foo;
Foobar() { $this->foo = 'something'; }
function myfunc()
{
// These are two different variables.
$bar = 'foo';
$this->bar = 'foo';
echo $this->foo;
}
}
There are some other good examples in the Classes and Objects
documentation at http://www.php.net/manual/html/oop.html
HTH,
Matt