parent Cannot Be Used Anymore In Callable Arrays

PHP supports a callable syntax, based on array: it must be an array of two elements, where the index 0 is the object or the class, and the index 1 is the method name.

Until PHP 8.2, it was possible to use the keyword parent, to make the callable work with the parent class.

In the example, parent would be calling the static method replace, in A, or any other parent.

Since PHP 8.2, this is a deprecated feature, and it will be removed in PHP 9.

PHP code

<?php

class A {
    public static function replace($a) {
     return 'a';
    }
}

class B extends A
{
    public static function work($it) {
             return preg_replace_callback('~\w+~', array('parent', 'parent::replace'), $it);
    }
}

echo b::work('abc');

?>

Before

PHP Fatal error:  Uncaught TypeError: preg_replace_callback(): Argument #2 ($callback) must be a valid callback, cannot access "parent" when current class scope has no parent

Fatal error: Uncaught TypeError: preg_replace_callback(): Argument #2 ($callback) must be a valid callback, cannot access parent when current class scope has no parent

After

PHP Deprecated:  Use of "parent" in callables is deprecated

Deprecated: Use of parent in callables is deprecated
PHP Fatal error:  Uncaught TypeError: preg_replace_callback(): Argument #2 ($callback) must be a valid callback, cannot access parent when current class scope has no parent

Fatal error: Uncaught TypeError: preg_replace_callback(): Argument #2 ($callback) must be a valid callback, cannot access parent when current class scope has no parent

PHP version change

This behavior was deprecated in 8.2

This behavior changed in 9.0

Error Messages

Analyzer