How does the use of static:: in PHP impact the class context in which a method is called and the results returned by get_called_class()?
When using static:: in PHP, the method is resolved based on the class where it is called, rather than the class where it is defined. This can impact the class context in which the method is executed and the results returned by get_called_class(). To ensure that get_called_class() returns the expected class name, you should use static::class instead of get_called_class().
class ParentClass {
public static function whoAmI() {
return static::class;
}
}
class ChildClass extends ParentClass {
}
echo ChildClass::whoAmI(); // Output: ChildClass
Keywords
Related Questions
- What are some common pitfalls to avoid when optimizing PHP code for performance, especially premature optimization?
- Are there any best practices or recommended functions in PHP for calculating date differences?
- What are the potential security risks of using outdated extensions like MySQL in PHP scripts?