上面咱们举个栗子:
class Father { public static function getSelf() { return new self(); } public static function getStatic() { return new static(); } } class Son extends Father {} echo get_class(Son::getSelf()); // Father echo get_class(Son::getStatic()); // Son echo get_class(Father::getSelf()); // Father echo get_class(Father::getStatic()); // Father
new self
这外面留意这一行 get_class(Son::getStatic()); 前往的是 Son 这个 class, 能够总结以下:
self 前往的是 new self 中要害字 new 所正在的类中,比方这里例子的 :
public static function getSelf() { return new self(); // new 要害字正在 Father 这里 }
始终前往 Father。
new static
static 则下面的根底上,更聪慧一点点:static 会前往执行 new static() 的类,比方 Son 执行 get_class(Son::getStatic()) 前往的是 Son, Father 执行 get_class(Father::getStatic()) 前往的是 Father
而正在不承继的状况下,能够以为 new self 以及 new static 是前往相反的后果。
以上就是PHP 的 new static 以及 new self的具体内容,更多请存眷资源魔其它相干文章!
标签: php php开发教程 php开发资料 php开发自学
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
抱歉,评论功能暂时关闭!