It's worth mentioning that the get_called_class
function came to exist from PHP's 5.3 version.
In addition to the difference in performances as quoted by Rafael
class Foo {
public static function bar()
{
echo "\n get_called_class: ";
$time = microtime(true);
echo get_called_class(). ' ';
printf('%f', microtime(true) - $time);
echo "\n static: ";
$time = microtime(true);
echo static::class . ' ';
printf('%f', microtime(true) - $time);
}
}
echo "<pre>";
Foo::bar();
Result:
get_called_class: Bar 0.000006
static: Bar 0.000001
There is another question that needs to be raised.
get_called_class
is used in partnership with late static binding
in frameworks.
For example, when a framework seeks to use a service locator / IoC system, they usually use get_called_class
because as this function returns the class name and this can be used to check if there are already instances together with the use of FactoryPattern
Example
class Factory
{
private static $_instances = array();
public static function getInstance()
{
$class = get_called_class();
if (!isset(self::$_instances[$class])) {
self::$_instances[$class] = new $class();
}
return self::$_instances[$class];
}
}
class ClassExtendFactory extends Factory {}
$class = ClassExtendFactory::getInstance();
$otherClass = ClassExtendFactory::getInstance();
var_dump($class === $otherClass);
// result true { ou seja, representam o mesmo objeto, duas variáveis apontando para o mesmo endereço de alocamento da memória }
When they do not use the factory, they only use late static binding
abstract class AbstractExample {
public static function getInstance() {
return new static();
}
}
class Example extends AbstractExample {
}
$ex1 = Example::getInstance();
$ex2 = Example::getInstance();
var_dump($ex1 === $ex2);
// return false { ou seja, agora são 2 instâncias diferentes da mesma class, duas variáveis apontando para espaços diferentes de alocamento na memória }
That's it, I hope it helped you understand a little better.