Static keyword is used to declare class members (properties and methods) that belong to the class itself rather than instances of the class. Here's a simple explanation
1. Static Properties:
- Static properties are shared among all instances of a class.
- They are declared using the static keyword.
- You can access them using the class name followed by ::
<?php
class MyClass {
public static $count = 0;
}
echo MyClass::$count;
?>
2. Static Methods:
- Static methods can be called without instantiating the class.
- They are also declared using the static keyword.
- You can access them using the class name followed by ::
<?php
class MathUtils {
public static function add($a, $b) {
return $a + $b;
}
}
echo MathUtils::add(2, 3); // Calling static method
?>
The static keyword in PHP is used to declare class members that belong to the class itself rather than instances of the class.
It allows you to work with properties and methods that are shared across all instances of the class or can be accessed without creating an instance of the class.