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