The Resolution Difference Between self and static in PHP
When you define a static property in a parent class and attempt to access it from a child class, the behavior depends entirely on whether you use the self keyword or the static keyword. While they often yield identical results in basic scenarios, their underlying resolution mechanisms differ significantly, leading to divergent outcomes when inheritance is involved.
Static Binding vs. Late Static Binding
The fundamental distinction lies in how PHP resolves these keywords at runtime. The self keyword is bound to the class where it appears in the code. This is known as static binding. When you write self::$bar, PHP looks for the property $bar on the class where the method is defined, regardless of which subclass actually called the method.
Conversely, static utilizes late static binding. The resolution happens at runtime based on the actual class used to instantiate or call the method. If a child class calls a static method that uses static, PHP looks up the inheritance chain from the calling class to find the property.
A Simple Example with Inheritance
Consider a scenario where you have a parent class defining a configuration value and several child classes that override this value.
class BaseConfig
{
protected static $version = '1.0';
public static function getVersion()
{
echo self::$version . PHP_EOL;
echo static::$version . PHP_EOL;
}
}
class LegacyApp extends BaseConfig
{
protected static $version = '2.0';
}
class ModernApp extends BaseConfig
{
protected static $version = '3.0';
}
BaseConfig::getVersion(); // Output: 1.0 then 1.0
LegacyApp::getVersion(); // Output: 1.0 then 2.0
ModernApp::getVersion(); // Output: 1.0 then 3.0
In the output above, self::$version always prints '1.0', referencing the BaseConfig class definition. It ignores the overridden property in the child classes. However, static::$version respects the inheritance chain. When called via LegacyApp::getVersion(), it resolves to 2.0.
Why Use self?
Using self is appropriate when you want strict adherence to the class definition where the method resides. This pattern is common in factories that create instances of a specific base type without caring about child implementations, or when you need to enforce a shared configuration across the entire hierarchy regardless of overrides. It provides a stable reference point that does not shift based on who calls the method.
Why Use static?
Late static binding is essential for polymorphism in class hierarchies. If your architecture relies on child classes configuring themselves differently, static ensures that each subclass sees its own configuration data. This pattern is prevalent in plugin systems and service providers where behavior or state must be isolated per subclass.
Practical Trade-offs
One common pitfall occurs when a static method in a parent class tries to access a property that was intended to be overridden in children but isn't. Using self prevents accidental leakage of the parent's implementation logic into child contexts, which can be a safety feature if you want to prevent overrides. On the other hand, relying on static without understanding late static binding can lead to confusing bugs where a method returns data from an unexpected class in the hierarchy.
In frameworks like Laravel, this distinction is critical when dealing with service providers or facades. While Laravel often employs magic methods that abstract these details, understanding the core difference helps when writing custom traits or extending built-in classes. For instance, if you are building a generic repository pattern where each subclass defines its own connection settings, static ensures the correct connection is used per subclass.
Conclusion
The choice between self and static determines whether your code references the class definition statically or dynamically resolves based on the calling context. Use self for fixed references to the defining class, and use static when you need the method to behave polymorphically with respect to child classes.