Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A static property in PHP is a class-level variable bound to the class itself rather than to any specific instantiated object. Because it resides in the class definition’s memory space, its state is shared globally across all instances of that class, and it can be accessed without requiring object instantiation.

Declaration Syntax

Static properties are declared using the static keyword, which must follow an access modifier (public, protected, or private). Modern PHP (7.4+) supports strict type declarations for static properties.
class Configuration {
    public static string $environment = 'production';
    protected static int $maxRetries = 3;
    private static array $cache = [];
}

External Access

Outside of the class scope, public static properties are accessed using the class name followed by the Scope Resolution Operator (::, also known as Paamayim Nekudotayim) and the property variable name (including the $ symbol).
// Accessing and mutating a static property directly
Configuration::$environment = 'development';
echo Configuration::$environment;
Note: Attempting to access a static property using the object operator (->) on an instantiated object will result in an “Undefined property” warning.

Internal Access and Binding

Within the class hierarchy, static properties are accessed using specific keywords combined with the Scope Resolution Operator. PHP provides three keywords for internal static access, each with distinct binding behaviors:
  1. self:: (Early Binding): Resolves to the class in which the code is explicitly written. It does not account for inheritance overrides.
  2. static:: (Late Static Binding): Resolves to the class that was initially called at runtime. This allows child classes to override static properties and have inherited methods reference the child’s property.
  3. parent::: Resolves to the immediate parent class, bypassing any overrides in the current class.
class Base {
    protected static string $type = 'BaseType';

    public static function getSelfType(): string {
        return self::$type; // Binds to Base
    }

    public static function getStaticType(): string {
        return static::$type; // Binds to the calling class at runtime
    }
}

class Child extends Base {
    protected static string $type = 'ChildType';
}

echo Base::getSelfType();    // Outputs: BaseType
echo Child::getSelfType();   // Outputs: BaseType (Early Binding)

echo Base::getStaticType();  // Outputs: BaseType
echo Child::getStaticType(); // Outputs: ChildType (Late Static Binding)

State Mutation and Memory

Because static properties are not bound to an object’s lifecycle, their state persists for the duration of the PHP script’s execution. Mutating a static property alters the state for the entire class context.
class StateContainer {
    public static int $value = 0;
}

$instanceA = new StateContainer();
$instanceB = new StateContainer();

StateContainer::$value = 42;

// The state change is reflected globally, regardless of instances
echo StateContainer::$value; // Outputs: 42

Variable Class Names

PHP allows dynamic access to static properties using variable class names. The variable must contain the fully qualified class name as a string.
class TargetClass {
    public static string $status = 'active';
}

$className = 'TargetClass';
echo $className::$status; // Outputs: active
Master PHP with Deep Grasping Methodology!Learn More