假设以下内容:class A{ public static function new() { return new self(); }}class B extends A{}class C { public function main() { $b = B::new(); // it actually returns a new A -- how to get it to return a new B since B inherited that method? }}呼叫B::new(),实际上会传回新的A-由于B继承了该方法,如何获取它以返回新的B?
1 回答
![?](http://img1.sycdn.imooc.com/54586653000151cd02200220-100-100.jpg)
守着一只汪
TA贡献1872条经验 获得超3个赞
替换new self()为new static()(后期绑定):
public static function new()
{
return new static();
}
- 1 回答
- 0 关注
- 108 浏览
添加回答
举报
0/150
提交
取消