Understanding PHP Static Members: self::, parent::, and static::
This article explains PHP static members, how self::, parent::, and static:: differ in inheritance contexts, when to use static members, and provides clear code examples illustrating their distinct behaviors for method and property access.
In PHP, static members (methods and properties) belong to the class itself rather than to any instance, allowing access without creating an object, which is useful for sharing data or functionality across objects.
PHP provides three keywords— self:: , parent:: , and static:: —to access static members, each with a distinct mechanism, especially when inheritance is involved.
Static members are typically used to simulate global variables, provide utility methods, define class-level constants, or implement the singleton pattern.
To call a static method, use the :: operator followed by the method name. Example:
class MyClass {
public static function greet() {
echo "Hello, world!";
}
}
MyClass::greet(); // outputs: Hello, world!Static properties are accessed similarly with the :: operator. Example:
class MyClass {
public static $count = 0;
public static function incrementCount() {
self::$count++;
}
}
MyClass::incrementCount();
echo MyClass::$count; // outputs: 1self:: always refers to the class where the code is written, ignoring inheritance. Even if a subclass overrides a static method or property, self:: will still reference the parent class's version.
parent:: is used to call a static method or property from the immediate parent class, bypassing any overrides in the subclass.
static:: introduces late static binding; it resolves at runtime to the most derived class, allowing the call to be dynamically bound to the subclass that invoked it.
Example 1 – using self:: demonstrates that self::sayHello() inside class A calls A 's method even when invoked from subclass B , producing "Hello from A".
class A {
public static function sayHello() { return "Hello from A"; }
public static function test() { return self::sayHello(); }
}
class B extends A {
public static function sayHello() { return "Hello from B"; }
}
echo B::test(); // outputs: "Hello from A"Example 2 – using parent:: shows that parent::sayHello() in subclass B calls the parent class A 's method and concatenates its own output, resulting in "Hello from A and B".
class A { public static function sayHello() { return "Hello from A"; } }
class B extends A { public static function sayHello() { return parent::sayHello() . " and B"; } }
echo B::sayHello(); // outputs: "Hello from A and B"Example 3 – using static:: illustrates late static binding: although static::sayHello() is written in class A , when called via subclass B it resolves to B 's overridden method, outputting "Hello from B".
class A {
public static function sayHello() { return "Hello from A"; }
public static function test() { return static::sayHello(); }
}
class B extends A { public static function sayHello() { return "Hello from B"; } }
echo B::test(); // outputs: "Hello from B"The main differences are: self:: binds to the class where it is written, parent:: explicitly calls the immediate parent, and static:: uses late static binding to resolve to the runtime class.
Understanding these distinctions helps developers write more robust and maintainable object‑oriented PHP code, especially when dealing with complex inheritance hierarchies.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.