Created
May 30, 2013 10:40
-
-
Save muhqu/5677046 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| class A { | |
| function create() { | |
| return new static(); | |
| } | |
| } | |
| class B extends A { | |
| } | |
| class Caller { | |
| function call() { | |
| return B::create(); | |
| } | |
| } | |
| $a = A::create(); | |
| $b1 = B::create(); | |
| $b2 = Caller::call(); | |
| $c = new Caller; | |
| $b3 = $c->call(); | |
| var_dump( | |
| phpversion(), | |
| get_class($a), # A | |
| get_class($b1), # B | |
| get_class($b2), # B | |
| get_class($b3) # Caller WTF!!! | |
| ); | |
| /*** | |
| string(6) "5.3.21" | |
| string(1) "A" | |
| string(1) "B" | |
| string(1) "B" | |
| string(6) "Caller" | |
| ***/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I can confirm this behaviour.
My guess would be: In your example '$b3' is created with non static methods only. So there is no static context around for creation. When "new static()" is executed, PHP tries to "bubble" up the classes to find the last static scope to use that class name. But you do not have any static context at all, so PHP has to create one in the last class which is in your case "Caller". This might be abled to thread as a error. I would suggest a Bugreport....
Proof: With the callStatic() method, the correct B class is returned: