local-host 2011-03-04 17:41 采纳率: 100%
浏览 257
已采纳

新的自我 vs 新的静态

I am converting a PHP 5.3 library to work on PHP 5.2. The main thing standing in my way is the use of late static binding like return new static($options); , if I convert this to return new self($options) will I get the same results?

What is the difference between new self and new static?

转载于:https://stackoverflow.com/questions/5197300/new-self-vs-new-static

  • 写回答

3条回答 默认 最新

  • 三生石@ 2011-03-04 18:19
    关注

    will I get the same results?

    Not really. I don't know of a workaround for PHP 5.2, though.

    What is the difference between new self and new static?

    self refers to the same class in which the new keyword is actually written.

    static, in PHP 5.3's late static bindings, refers to whatever class in the hierarchy you called the method on.

    In the following example, B inherits both methods from A. The self invocation is bound to A because it's defined in A's implementation of the first method, whereas static is bound to the called class (also see get_called_class()).

    class A {
        public static function get_self() {
            return new self();
        }
    
        public static function get_static() {
            return new static();
        }
    }
    
    class B extends A {}
    
    echo get_class(B::get_self());  // A
    echo get_class(B::get_static()); // B
    echo get_class(A::get_self()); // A
    echo get_class(A::get_static()); // A
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集