PHP Static Properties
PHP Static Properties: Class-Level Shared State
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.
Declaring and Updating Static Properties
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;
Why Mutable Static State Needs Caution
While static properties look handy for simple in-memory caches or counters, mutable public static properties behave just like global variables:
- Hidden coupling in tests: A unit test that modifies
Config::$mode = 'test'leaks that value into subsequent tests unless reset manually. - Long-running worker leaks: Under Laravel Octane, Swoole, or RoadRunner, PHP stays alive across thousands of requests. Static caches accumulate endlessly in memory and leak data between users unless explicitly flushed.
Use private static only for safe per-process read-only lookups, or move state into an injected dependency container object.
Common Mistakes to Avoid
- Accessing a static property with
$this->prop: Static properties cannot be accessed with->in PHP;$this->proptriggers an undefined property warning. Always writeself::$prop. - Omitting the
$after::: WritingCounter::countlooks for a class constant instead of the static propertyCounter::$count. - Storing user-specific request data in static arrays: In queue workers or Octane servers, user A's cached data would bleed into user B's request.
Frequently Asked Questions
Do static properties persist across separate HTTP requests in standard PHP-FPM?
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.
Can a static property be initialized with a function call?
No. Static property defaults must be constant expressions (numbers, strings, arrays, booleans). Initialize dynamic values inside a static helper method on first access.
Can subclasses share or override a parent's protected static property?
Yes. Subclasses share the parent's static property unless the child class redeclares its own protected static $prop.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.