为了账号安全,请及时绑定邮箱和手机立即绑定

如何在继承的方法中引用子类?

如何在继承的方法中引用子类?

PHP
慕工程0101907 2021-05-07 18:30:19
假设以下内容: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 回答

?
守着一只汪

TA贡献1872条经验 获得超3个赞

替换new self()为new static()(后期绑定):


public static function new() 

{

    return new static();

}


查看完整回答
反对 回复 2021-05-21
  • 1 回答
  • 0 关注
  • 108 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信