PHP 8.3: get_class() and get_parent_class() function calls without arguments deprecated

Version8.3
TypeDeprecation

PHP 8.3 deprecates several function and class methods that support more than one signature. Historically, these functions initially accepted one function signature, but were updated in a later version to support another set of parameters without declaring a new function.

get_class and get_parent_class functions are some of the functions that also support two signatures.

Both functions accept an object $object parameter that returns the name of the class, or the name of the parent class (in case of get_parent_class function).

get_class(new stdClass()); // "stdClass"
get_parent_class(new \InvalidArgumentException()); // "LogicException"

However, it also supports an alternative signature that, when no parameters are passed, it returns the name of the class in context.

class MyException extends InvalidArgumentException {
    public function __construct() {
        get_class(); // "MyException"
        get_parent_class(); // "InvalidArgumentException"
    }
}

In PHP 8.3, calling get_class and get_parent_class functions without parameters is deprecated. The snippet above emits deprecation notices in PHP 8.3. The alternative overloaded signature will be removed in PHP 9.0, which results in an ArgumentCountError exception unless the object $object parameter is passed.

Deprecated: Calling get_class() without arguments is deprecated
Deprecated: Calling get_parent_class() without arguments is deprecated

Replacement for get_class()

Currently get_class() function is only allowed to be called within a class context, and the deprecated get_class call can be replaced either of self::class or __CLASS__ constants. The self::class magic constant is available in PHP 5.4 and later. The __CLASS__ constant is available since PHP 5.0 and later.

class Test {
    public function __construct() {
-       echo get_class();
+       echo __CLASS__;
    }
}

Alternately, passing $this to the get_class function avoids the deprecation notice while keeping the get_class call. This is supported in all PHP versions since 5.0.

class Test {
    public function __construct() {
-       echo get_class();
+       echo get_class($this);
    }
}

$object::class support in PHP 8.0 and later
Since PHP 8.0, the ::class magic constant is supported in objects as well. On PHP 8.0 applications, the get_class call can be replaced safely with $object::class.

- get_class($object);
+ $object::class

See PHP 8.0: ::class magic constant is now supported on objects.


Replacement for get_parent_class()

get_parent_class() calls with no parameters return the same value as parent::class magic constant. Such function calls can be replaced with parent::class constant.

Similar to the get_class replacement, passing $this to get_parent_class() works, however, this behavior is not identical to get_parent_class because using parent when there is no parent class results in a fatal error.

class Test extends BaseTest{
    public function __construct() {
-       echo get_parent_class();
+       echo parent::class;
    }
}

To mimic get_parent_class() behavior without deprecation notices, pass $this to the function:

class Test extends BaseTest{
    public function __construct() {
-       echo get_parent_class();
+       echo get_parent_class($this);
    }
}

Backward Compatibility Impact

PHP 8.3 deprecates calling get_class and get_parent_class functions with no parameters. This is to prepare PHP's code base to not support overloaded alternative function signatures.

get_class and get_parent_class calls can be safely replaced with backward-compatible alternatives to avoid the deprecation notice.


RFC Discussion Implementation