PHP self 指向定义的 class。
PHP static 指向运行的 class,一般只有子类覆盖父类的 static 成员或者方法时,在父类中使用 static 会访问到子类。
class ParentClass
{
public static function hello()
{
echo "ParentClass: hello\n";
}
public static function run()
{
self::hello();
static::hello();
}
}
class ChildClass extends ParentClass
{
public static function hello()
{
echo "ChildClass: hello\n";
}
}
ParentClass::run();
// 输出
"ParentClass: hello"
"ParentClass: hello"
ChildClass::run();
// 输出
"ParentClass: hello"
"ChildClass: hello"
作者: 袖之欢
科技改变生活,编程改变世界。查看袖之欢的所有文章