Created
August 12, 2011 04:06
-
-
Save damncabbage/1141432 to your computer and use it in GitHub Desktop.
$this weirdness when calling non-static function as if they were static.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// | |
// Call a non-static function statically from another object, | |
// and $this is left set as if we were still in the calling function. | |
// | |
class Foo { | |
public function notStatic() { | |
echo get_class($this)."\n"; | |
} | |
} | |
class Bar { | |
public function regularMethod() { | |
Foo::notstatic(); | |
} | |
} | |
$bar = new Bar(); | |
$bar->regularMethod(); // Prints out "Bar". What the hell? | |
Foo::notStatic(); // Explodes, because $this is not defined. | |
// Verdict: | |
// - Internals | |
// - Hacky pseudo-closures in PHP 5.2! :D | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment