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 is the difference between single and double quotes in PHP and how does it affect variable parsing?
- What are the best practices for handling multiple instances of MySQLHandlers in PHP models?
- How can PHP developers ensure that XML files generated from HTML content maintain their validity and structure?