In PHP, the static
keyword is used to declare static properties and methods in a class.
Static properties are shared among all instances of a class and can be accessed without creating an object of the class. They are defined using the static
keyword before the property name. For example:
class MyClass {
public static $myStaticProperty = 'Hello';
public static function myStaticMethod() {
echo self::$myStaticProperty;
}
}
echo MyClass::$myStaticProperty; // Output: Hello
MyClass::myStaticMethod(); // Output: Hello
Static methods are also shared among all instances of a class and can be called without creating an object of the class. They are defined using the static
keyword before the method name. For example:
class MyClass {
public static function myStaticMethod() {
echo 'Hello';
}
}
MyClass::myStaticMethod(); // Output: Hello
It's important to note that static properties and methods cannot access non-static properties and methods directly. They can only access other static properties and methods.
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站