Last active
August 29, 2015 14:06
-
-
Save l-x/08e14a5060495add5842 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* @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) { | |
* | |
* } | |
* | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This problem (feature?) already exists for a while: https://bugs.php.net/bug.php?id=50031