🐘 PHP Intermediate

What is the static keyword in PHP?

Answer

The static keyword in PHP has multiple uses. Static class members (properties and methods) belong to the class rather than an instance — access with ClassName::$property or ClassName::method(). Static variables inside functions retain their value between function calls: function counter() { static $count = 0; return ++$count; }. Late static binding (using static:: instead of self::) refers to the class that was actually called at runtime, not where the method was defined — crucial for inheritance scenarios where you want to reference the child class from a parent method. self:: always refers to the class where the code is written; static:: refers to the runtime class.