On class properties
<?php
class foo {
static public $bar = 2;
}
print foo::$bar;
$n = new foo;
print $n->bar;
print $n->$bar;
?>Results?
2
PHP Notice: Undefined property: foo::$bar
PHP Notice: Undefined variable: bar
PHP Fatal error: Cannot access empty property Moral of the story: making a property static means that it's mandatory to reach it via the :: operator. It's not accessible as a normal property any more.

Comments
Of course
It's not the same variable. It's bound to a totally different scope. statics are just globals with restricted access and a funny hat.
This isn't a WTF, this is the way any reasonable language should work. (This was not the case in PHP 4, which was not a reasonable language where anything involving objects is concerned.)
Some languages (Java) allow
Some languages (Java) allow you to access static members though an instance, but it's the exception rather than the rule. PHP is more correct (and more explicit) here.
Post new comment