new static 跟 new self 區別
阿新 • • 發佈:2019-02-06
<?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) { }