PHP中new static()与new self()的区别及5.2版本重写5.3

来源:转载

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_static()); // A

5.2替换5.3 return new static($val); 

用 get_class($this); 代码如下:

class A {

  public function create1() {
    $class = get_class($this);
return new $class();
  }
  public function create2() {
    return new static();
  }
}


class B extends A {

}


$b = new B();
var_dump(get_class($b->create1()), get_class($b->create2()));


/*
The result 
string(1) "B"
string(1) "B"

*/

参考http://www.jb51.net/article/54167.htm

分享给朋友:
您可能感兴趣的文章:
随机阅读: