Skip to content

Instantly share code, notes, and snippets.

@muhqu
Created May 30, 2013 10:40
Show Gist options
  • Select an option

  • Save muhqu/5677046 to your computer and use it in GitHub Desktop.

Select an option

Save muhqu/5677046 to your computer and use it in GitHub Desktop.
<?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"
***/
@muhqu
Copy link
Copy Markdown
Author

muhqu commented May 30, 2013

Node: if you add static before function create() everything works as expected....

But STILL, if new static() is suposed to only be used within a static method, then please throw an exception if its used in a non-static method!

@h4cc
Copy link
Copy Markdown

h4cc commented May 31, 2013

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:

<?php
//...

class Caller {
  function call() {
    return B::create();
  }

  static function callStatic() {
    return B::create();
  }
}

$a = A::create();
$b1 = B::create();
$b2 = Caller::call();
$c = new Caller;
$b3 = $c->call();
$b4 = $c::callStatic();
$b5 = $c->callStatic();

var_dump(
  phpversion(),
  get_class($a),
  get_class($b1),
  get_class($b2),
  get_class($b3),
  get_class($b4),
  get_class($b5)
);

/***
string(16) "5.4.6-1ubuntu1.2"
string(1) "A"
string(1) "B"
string(1) "B"
string(6) "Caller"
string(1) "B"
string(1) "B"
***/

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