I was explaining to a Java developer why his method call wasn\'t working. He just needed to add $this->method_name();
He then asked me, \"Why do
That's just how scope works in PHP. $obj->f()
refers to $foo
in the function scope. If you want to get the class property $obj->foo
within f()
, it's $this->foo
.
global $foo;
$foo = 99;
class myclass
{
public $foo;
function f()
{
$this->foo = 12;
$foo = 7;
// $this->foo != $foo != $GLOBALS['foo']
}
}