PHP self 和 static 区别

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"

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注