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

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options