Getting the Child Class Name from a Static Call in PHP
When you define a method in a parent class like Action and intend for it to dynamically return the name of whatever child class calls it, you run into a fundamental limitation of how PHP resolves static variables. The variable __CLASS__ is evaluated at compile time within the context of the class where it is defined. If your base class looks like this:
class Action {
public static function n() {
return __CLASS__; // Returns "Action"
}
}
and you call MyAction::n(), the output remains Action. This happens because the bytecode for the Action class is fixed; it doesn't inspect the runtime context of the caller when a static method is invoked. The static binding in PHP prevents the parent from "seeing" the specific child instance or class name during a standard static call without additional architectural changes.
To achieve the behavior where MyAction::n() returns "MyAction" while keeping the logic in Action, you must move away from pure static evaluation and introduce reflection or override the method in the child. The most robust, framework-agnostic solution involves using ReflectionClass.
You can implement this by having the parent class accept a hint about the current class context or by leveraging the fact that static methods are resolved via the calling namespace. However, since static calls bypass the object hierarchy directly, Action cannot simply ask "who called me?" in a standard way.
A practical approach that keeps the implementation strictly within the Action class (as requested) relies on passing the current class name as an argument or utilizing a trait that provides dynamic context. But if you strictly require zero arguments and want the child to be identified automatically, you are limited by PHP's static analysis rules unless you utilize Reflection in a specific way that requires the child to participate.
Wait, there is a nuance here. If Action defines the method as public static function n(), calling it on a child class invokes the parent's method logic. Inside that logic, __CLASS__ is hardcoded to "Action". To get the actual calling class name without modifying every child class, you can use ReflectionFunction::getClosureScopeClass? No, that applies to closures. For static methods, the function object points to the definition site.
The correct solution that fits the constraint of keeping logic in Action but returning the child's name involves using get_called_class(). Unlike __CLASS__, which is resolved at compile time based on where the code lives, get_called_class() is evaluated at runtime based on which class actually invoked the method.
Here is how you modify the Action class:
class Action {
public static function n() {
return get_called_class();
}
}
When you execute MyAction::n(), PHP executes the body of the Action::n() method, but it resolves get_called_class() against the static context of the caller. Since MyAction is making the call, get_called_class() returns "MyAction". If you subsequently extend that into AnotherAction and call AnotherAction::n(), it correctly returns "AnotherAction".
This function was introduced in PHP 5.2.3 and has been stable since then. It is specifically designed for this exact scenario: dynamic polymorphism in static contexts. The trade-off is a slight performance overhead compared to using __CLASS__ directly, but that is negligible in modern PHP versions (7.4+).
A common pitfall developers encounter is confusing get_called_class() with static::class. While both can return the child class name when used inside an overridden method or a trait context, get_called_class() works even when the method is defined entirely in a parent class and never overridden. If you use static::class inside the parent method without overriding it, it often still resolves to the parent depending on how the function is bound, though in modern PHP 8 with late static binding, the behavior can sometimes be tricky if not careful. However, get_called_class() is explicitly documented to return the class that called the method, making it the safest choice for this specific requirement.
If you are working within the Laravel ecosystem, this pattern is frequently useful when building generic event listeners or base controllers. You might see similar patterns in packages like those listed on https://laravelcompany.com, where a base trait needs to identify the specific model or controller invoking a shared hook without forcing every subclass to pass its name as a string argument.
The implementation is simple: replace __CLASS__ with get_called_class() in your parent class definition. No arguments, no traits required in the children, and no duplication of logic. Just ensure you are running on PHP 5.2 or later, which covers virtually every production environment today.