Over the weekend i encountered a twist in PHP that really left me wondering. I made a mistake and i thought i should have gotten an error, or at least a warning. I got a completely unexpected behavior instead.
According to our PHP Oracle this is just a legacy from PHP 4 though and there was much Discussion wether changing this behavior would break old apps. I think it’s a possible source of hard-to-track errors though.Consider the following snippet:
<?php error_reporting(E_ALL); class a { public function bar() { $b = new b(); b::foo("foobar"); // error, this method is not static } public function mprint($what) { echo "not ".$what."\n"; } } class b { public function foo($what) { $this->mprint($what); } public function mprint($what) { echo $what."\n"; } } $a = new a(); $a->bar(); ?>
What would one expect? Probably an error, or at least a warning.
What does it do?
~> /usr/local/bin/php mytest.php not foobar
What happened? The instance of class a has a reference to $this. Since we do not use $b, $this in method foo() still points to the instance of class a. If class a had no method mprint(), PHP would complain about the missing method. Too bad there is not a not-static keyword.
The aforementioned Oracle told me that this will not work in PHP 6 though, since all methods not declared static are non-static there.
Weitere Artikel wie Spooky Action at not so much Distance finden Sie unter Mayflower Blog.