Skip to content

Instantly share code, notes, and snippets.

@damncabbage
Created August 12, 2011 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damncabbage/1141432 to your computer and use it in GitHub Desktop.
Save damncabbage/1141432 to your computer and use it in GitHub Desktop.
$this weirdness when calling non-static function as if they were static.
<?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