Calling get_class Without Argument is Deprecated in PHP 8.3

Calling get_class Without Argument is Deprecated in PHP 8.3

The get_class is a built-in PHP function that is used to retrieve the name of the class of an object. This function is particularly useful when we need to dynamically determine the class of an object during runtime. This function inside the class can be called without argument to get the name of the class.

Let's say we have the following class:

Logger.php

<?php

class Logger
{
    public static function getTag(): string
    {
        return get_class();
    }
}

Since PHP 8.3, the following code emits a deprecation warning because the get_class function is called without argument:

<?php

require_once 'Logger.php';

echo Logger::getTag();
Deprecated: Calling get_class() without arguments is deprecated in Logger.php on line 7
Logger

To avoid deprecation, the class can be rewritten using __CLASS__ magic constant:

Logger.php

<?php

class Logger
{
    public static function getTag(): string
    {
        return __CLASS__;
    }
}

It's important to be aware that using the get_parent_class function without providing an argument is also deprecated.

Leave a Comment

Cancel reply

Your email address will not be published.