PHP Static Keyword: Uses Explained

In PHP, the static keyword is used to declare static properties and methods. Static properties and methods can be accessed directly without instantiating a class, while non-static properties and methods need to be accessed through class instantiation.

Static properties are shared throughout the entire class, and all instantiated objects can access and modify them. Similarly, static methods are also part of the class and do not belong to any instantiated object, allowing them to be called directly through the class name.

Here is an example of using the static keyword:

class MyClass {
    public static $staticProperty = 'I am a static property';

    public static function staticMethod() {
        return 'I am a static method';
    }
}

echo MyClass::$staticProperty; // 输出:I am a static property
echo MyClass::staticMethod(); // 输出:I am a static method
bannerAds