1. 程式人生 > >PHP中new static() 和 new self() 的區別

PHP中new static() 和 new self() 的區別

pub 堆內存 func sel urn ret 通過 ati php

self 指的是self所在的類

new static 實例化的是當前使用的類,有點像$this ,從堆內存中提取出來。

還是通過實例說明一下:

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

PHP中new static() 和 new self() 的區別