Can the __CLASS__ keyword be defined or accessed in a parent class and return the child class it is called from?
The __CLASS__ keyword in PHP cannot be defined or accessed in a parent class to return the child class it is called from. However, you can achieve similar functionality by using late static binding with the static::class keyword. This allows you to access the class name of the child class where the method is called.
class ParentClass {
public function getClass() {
return static::class;
}
}
class ChildClass extends ParentClass {
}
$child = new ChildClass();
echo $child->getClass(); // Output: ChildClass
Related Questions
- How can one check the status of allow_url_fopen in PHP?
- How can PHP developers efficiently handle user input validation and sanitization to prevent common vulnerabilities like SQL injection and cross-site scripting (XSS)?
- In PHP, what are some common pitfalls to avoid when working with arrays and database interactions for data management?