温馨提示×

php self如何调用静态属性

PHP
小樊
83
2024-07-13 01:52:26
栏目: 编程语言

在PHP中,可以使用self::关键字来调用静态属性。例如:

class Example {
    public static $staticProperty = "Static Property";

    public function getStaticProperty() {
        return self::$staticProperty;
    }
}

echo Example::$staticProperty; // 输出 "Static Property"

$example = new Example();
echo $example->getStaticProperty(); // 输出 "Static Property"

在上面的示例中,我们使用self::$staticProperty来调用静态属性$staticProperty。无论是在类的内部还是外部,都可以使用self::关键字来访问静态属性。

0