new static 跟 new self 区别

发布时间:2020-06-30 00:50:36 作者:lijingsan1
来源:网络 阅读:341
<?php
/**
 * Created by PhpStorm.
 * User: 27394
 * Date: 2016/11/15
 * Time: 8:35
 */
class A{
    public static function __contruct(){
        echo __CLASS__."A";
    }
    public static function init(){
        return new self();
    }
}
class B{
    public static function __contruct(){
        echo __CLASS__."B";
    }
    public static function init(){
        return new static();
    }
}
class C extends A{
    public static function __contruct(){
        echo __CLASS__."C";
    }
}
class D extends B{
    public static function __contruct(){
        echo __CLASS__."D";
    }
}
class E extends A{
    public static function __contruct(){
        echo __CLASS__."E";
    }
    public static function init(){
        return new self();
    }
}
class F extends B{
    public static function __contruct(){
        echo __CLASS__."F";
    }
    public static function init(){
        return new static();
    }
}
var_dump(C::init());// new self()
// object(A)#1 (0) { } self 代表 使用它的那个类
var_dump(D::init());// new static()
// object(D)#1 (0) { } static 代表当前自己的类
var_dump(E::init());
//object(E)#1 (0) { }
var_dump(F::init());
//object(F)#1 (0) { }


推荐阅读:
  1. PHP new self()和new static()的区别
  2. PHP new self和new static的区别

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php.static.self sel st

上一篇:Android Studio 使用中遇到的问题解决

下一篇:恢复电脑误删除文件的小技巧

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》