self 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 self, to make the callable dependent on the context of usage of the callable.
In the example, self would be calling the static method replace, in A.
Since PHP 8.2, this is a deprecated feature, and it will be removed in PHP 9.
PHP code¶
<?php
class B
{
public static function work($it) {
return preg_replace_callback('~\w+~', array('self', 'self::replace'), $it);
}
public static function replace($a) {
return 'a';
}
}
echo b::work('abc');
?>
Before¶
a
After¶
PHP Deprecated: Use of "self" in callables is deprecated
Deprecated: Use of "self" in callables is deprecated
PHP Deprecated: Callables of the form ["B", "self::replace"] are deprecated
Deprecated: Callables of the form ["B", "self::replace"] are deprecated
a
PHP version change¶
This behavior was deprecated in 8.2
This behavior changed in 9.0