PHP Initializing a new instance of a subclass from a parent static method?
How would I initialize a subclass from a parent static method?
I could get subclass's static variable using late static binding in PHP 5.3 and the static
keyword in a parent class's methods. How would I initialize a new instance of a subclass in 开发者_JS百科a parent static method?
Thanks.
You don't need to involve static variables or anything that messy. Here's the shortest example I can come up with:
class a {
public static function foo() { return new static; }
}
class b extends a { }
if you call b::foo()
you will get a new b.
精彩评论