Difference between __CLASS__, get_class, and get_called_class
suggest change__CLASS__ is the class in which the method was written. Calling get_class() with no arguments used to return that same defining class, not the class that was instantiated.
Since PHP 8.3, get_class() with no arguments is deprecated, and PHP 9 removes it. Use __CLASS__ instead.
get_class($this) returns the class of the current object. get_called_class() returns the class the method was called on, which matters for late static binding. In the example below those two names match because say() is an instance method called on Actual_Class:
<?php
class Definition_Class {
public function say(){
echo '__CLASS__ value: ' . __CLASS__ . "\n";
echo 'get_called_class() value: ' . get_called_class() . "\n";
echo 'get_class($this) value: ' . get_class($this) . "\n";
echo 'get_class() value: ' . get_class() . "\n";
}
}
class Actual_Class extends Definition_Class {}
$c = new Actual_Class();
$c->say();
// Output:
// __CLASS__ value: Definition_Class
// get_called_class() value: Actual_Class
// get_class($this) value: Actual_Class
// get_class() value: Definition_Class
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents