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 in /codes/parentInCallable.php:12
Stack trace:
#0 /codes/parentInCallable.php(12): preg_replace_callback('~\w+~', Array, 'abc')
#1 /codes/parentInCallable.php(16): B::work('abc')
#2 {main}
thrown in /codes/parentInCallable.php on line 12
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 in /codes/parentInCallable.php:12
Stack trace:
#0 /codes/parentInCallable.php(12): preg_replace_callback('~\w+~', Array, 'abc')
#1 /codes/parentInCallable.php(16): B::work('abc')
#2 {main}
thrown in /codes/parentInCallable.php on line 12
After¶
PHP Deprecated: Use of parent in callables is deprecated in /codes/parentInCallable.php on line 12
Deprecated: Use of parent in callables is deprecated in /codes/parentInCallable.php on line 12
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 in /codes/parentInCallable.php:12
Stack trace:
#0 /codes/parentInCallable.php(12): preg_replace_callback('~\w+~', Array, 'abc')
#1 /codes/parentInCallable.php(16): B::work('abc')
#2 {main}
thrown in /codes/parentInCallable.php on line 12
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 in /codes/parentInCallable.php:12
Stack trace:
#0 /codes/parentInCallable.php(12): preg_replace_callback('~\w+~', Array, 'abc')
#1 /codes/parentInCallable.php(16): B::work('abc')
#2 {main}
thrown in /codes/parentInCallable.php on line 12
PHP version change¶
This behavior was deprecated in 8.2
This behavior changed in 9.0