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
- What alternatives to mysql_escape_string can be used to escape special characters in PHP without affecting line breaks?
- What are some common issues with the mail() function in PHP, such as receiving a TRUE response but the email not being delivered?
- How can one effectively access individual arrays after using array_chunk in PHP?