Static properties and variable classnames

Oh no!

<?php
class test {
  static public
$foo = 'test1';
  static public
$bar = 'test2';
  function
foo() {
    print
"method foo\n";
  }
}

class
test1 {
  function
__construct() {
    print
"test1 constructor\n";
  }
}
class
test2 {
  function
__construct() {
    print
"test2 constructor\n";
  }
}

function
test1() {
  print
"test1 function\n";
}
function
test2() {
  print
"test2 function\n";
}
function
foo() {
  print
"foo function\n";
  return
'bar';
}

$foo = 'foo';

$a = test::$foo;
new
$a();
new
test::$foo();
$a();
print
test::$foo();
?>

test1 constructor
test1 constructor
test1 function
method foo

Comments

Wow

This does my head in, though it would help if the sample were a little cleaner. There seems to be much unused code in there. All the first parts seem to make a lot of sense, given the idea of variable variables. For the life of my, I can't figure out why that last one should result in the method call though. That's the only one that seems unintuitive.

Binding precedence

In the last line, $foo is being evaluated to the string 'foo' first, and then test::foo() is being called. That part makes sense.

The oddball here is that A::$b apparently means "Static variable b of class A" if there is no $b in scope at the time, but "Static variable the value of b in class A" if there is. That is, $ binds higher than :: does.

$ binding higher than :: explains all of the above code quite readily, even if it ends up looking bizarre. The alternative would make it impossible to call a variable class method or class property.

That said, I now have to wonder if there aren't security implications there...

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