1
我在交响乐看到Symfony\Component\HttpFoundation\Request(行1922)等建设在symfony中构建新静态()是什么意思?
return new static($query, $request, $attributes, $cookies, $files, $server, $content);
我无法google一下。这是什么意思?
我在交响乐看到Symfony\Component\HttpFoundation\Request(行1922)等建设在symfony中构建新静态()是什么意思?
return new static($query, $request, $attributes, $cookies, $files, $server, $content);
我无法google一下。这是什么意思?
当你写一个类的成员函数中新自(),你得到这个类的一个实例。这是自我关键字的魔力。
So:
class Foo
{
public static function baz() {
return new self();
}
}
$x = Foo::baz(); // $x is now a `Foo`
你得到一个Foo,即使你使用的静态预选赛是派生类:
class Bar extends Foo
{
}
$z = Bar::baz(); // $z is now a `Foo`
如果要启用多态性(在一定意义上),并且具有的PHP留意了您可以使用self关键字替换静态关键字:
class Foo
{
public static function baz() {
return new static();
}
}
class Bar extends Foo
{
}
$wow = Bar::baz(); // $wow is now a `Bar`, even though `baz()` is in base `Foo`
这是通过称为迟静态绑定的PHP功能实现的;不要将其与其他关键字static的更传统用法混淆。
请参阅http://stackoverflow.com/a/5197655/582278 –
[后期静态绑定](http://php.net/manual/en/language.oop5.late-static-bindings.php) – raina77ow