开发者

PHP Initializing a new instance of a subclass from a parent static method?

开发者 https://www.devze.com 2023-03-18 16:00 出处:网络
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 wo

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消