How can the use of __CLASS__ in PHP differ between static and instantiated function calls?
When using __CLASS__ in PHP, it refers to the class in which it is used. When used in a static context, such as within a static function, __CLASS__ will always refer to the class in which the static function is defined. However, when used in an instantiated context, such as within a non-static method, __CLASS__ will refer to the class of the object instance that the method is called on.
class MyClass {
public static function staticFunction() {
echo __CLASS__; // Outputs MyClass
}
public function instanceFunction() {
echo __CLASS__; // Outputs MyClass
}
}
MyClass::staticFunction();
$obj = new MyClass();
$obj->instanceFunction();