A PHP static property is a variable attached to a class definition rather than an individual object instance, declared with public static or private static. Every instance of the class—and any code referencing ClassName::$property—reads and modifies that exact same shared value in memory.
Always include the $ sign when accessing a static property after :: (for example, self::$count or Counter::$count), unlike class constants which omit $.
<?php
declare(strict_types=1);
class RateLimiterConfig
{
public static int $maxRequestsPerMinute = 60;
private static int $checkedCount = 0;
public static function recordCheck(): int
{
return ++self::$checkedCount;
}
}
echo "Limit: " . RateLimiterConfig::$maxRequestsPerMinute . "/min" . PHP_EOL;
echo "Check #" . RateLimiterConfig::recordCheck() . PHP_EOL;
echo "Check #" . RateLimiterConfig::recordCheck() . PHP_EOL;
While static properties look handy for simple in-memory caches or counters, mutable public static properties behave just like global variables:
Config::$mode = 'test' leaks that value into subsequent tests unless reset manually.Use private static only for safe per-process read-only lookups, or move state into an injected dependency container object.
$this->prop: Static properties cannot be accessed with -> in PHP; $this->prop triggers an undefined property warning. Always write self::$prop.$ after ::: Writing Counter::count looks for a class constant instead of the static property Counter::$count.No. In traditional PHP-FPM/Apache setups, all memory—including static properties—resets cleanly at the end of every HTTP request. Only long-lived CLI workers (Octane, queues) retain static state.
No. Static property defaults must be constant expressions (numbers, strings, arrays, booleans). Initialize dynamic values inside a static helper method on first access.
protected static property?Yes. Subclasses share the parent's static property unless the child class redeclares its own protected static $prop.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.