Skip to content

Instantly share code, notes, and snippets.

@l-x
Last active August 29, 2015 14:06
Show Gist options
  • Save l-x/08e14a5060495add5842 to your computer and use it in GitHub Desktop.
Save l-x/08e14a5060495add5842 to your computer and use it in GitHub Desktop.
<?php
/**
* @link https://l-x.github.io/blog/2014/09/29/messing-around-with-phps-late-static-binding/
*/
class A {
public function createInstance() {
/**
* Calling method static even if
* defined non-static in class B
*/
return B::createInstance();
}
}
class B {
public function createInstance() {
return new static();
}
}
$instance_of_A = new A();
$expected_instance_of_B = $instance_of_A->createInstance();
var_dump($expected_instance_of_B);
/**
* Expected:
*
* class B#2 (0) {
*
* }
*
* Result:
*
* class A#2 (0) {
*
* }
*
*/
@l-x
Copy link
Author

l-x commented Sep 19, 2014

This problem (feature?) already exists for a while: https://bugs.php.net/bug.php?id=50031

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment