ITPub博客

首页 > IT职业 > IT生活 > PHP new self()和new static()的区别

PHP new self()和new static()的区别

IT生活 作者:nt1979 时间:2021-09-09 09:29:14 0 删除 编辑

new static()是php5.3以后引入新的特性,延迟静态绑定.访问的是当前实例化的那个类,那么 static 代表的就是那个类。

new self() 是指的不是调用上下文,它指的是解析上下文.

class  Test {

    public static funtion getSelf(){

            return new self();

     }

    

    public static funtion getStatic(){

            return new static();

     }

  }

class  Test1 extends Test {


  }

echo get_class(Test1 ::getSelf);  输出:Test 

echo get_class(Test1 ::getStatic);输出:Test1 

echo get_class(Test ::getStatic);输出:Test 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4822/viewspace-2810142/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-05

  • 博文量
    318
  • 访问量
    17307