PHP中new self() 以及 new static() 的区分
new static() 是正在php5.3版本引入的新特点
new static 以及 new self() 都是 new 一个工具
间接看代码
class Father { public function getNewFather() { return new self(); } public function getNewCaller() { return new static(); } } $f = new Father(); var_dump(get_class($f->getNewFather())); // Father var_dump(get_class($f->getNewCaller())); // Father
getNewFather以及getNewCaller 都是前往的 Father 这个实列
到这里貌似 new self() 仍是 new static() 是不区分的
接着看上面的示例
class Sun1 extends Father{ } $sun1 = new Sun1(); var_dump($sun1->getNewFather()); // object(Father)#4 (0) { } var_dump($sun1->getNewCaller()); // object(Sun1)#4 (0) { }
getNewFather 前往的是Father的实列,
getNewCaller 前往的是挪用者的实列
他们的区分只有正在承继中能力表现进去、假如不任何承继、那末两者不任何区分
new self() 前往的实列是没有会变的,无论谁去挪用,都前往的一个类的实列,
new static则是由挪用者决议的。
保举教程:《PHP视频教程》
以上就是区分PHP中new self() 以及 new static()的具体内容,更多请存眷资源魔其它相干文章!
标签: php开发教程 php开发资料 php开发自学 new self() new static()
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
抱歉,评论功能暂时关闭!