php区分new static 和new self

时间:2021-11-11 14:32:20

关键点在于一个是静态绑定,一个是延迟绑定

<?php

class A
{
public function __construct() {

}

public function createObjStatic() {
$obj = new self();
$obj->say();
}

public function createObjDynamic() {
$obj = new static();
$obj->say();
}

public function say() {
echo "I'm class A.";
}
}

class B extends A
{
public function __construct() {

}

public function say() {
echo "I'm class B.";
}
}

$B = new B();
$B->createObjDynamic();
echo "<BR>";
$B->createObjStatic();

?>