Returning null from %s::__debugInfo() is deprecated, return an empty array instead¶
Description¶
The __debugInfo() magic method allows a class to customize what var_dump() displays for its instances. When this method returns null, PHP has historically treated it as an empty array, but the behavior was inconsistent.
Since PHP 8.4, returning null from __debugInfo() is deprecated. This deprecation ensures consistent behavior and explicit return types.
Returning an empty array [] achieves the same effect (hides all properties from var_dump()) while being type-safe and unambiguous.
Example¶
<?php
class X {
public function __debugInfo(): ?array {
return null;
}
}
var_dump(new X);
?>
Literal Examples¶
Returning null from X::__debugInfo() is deprecated, return an empty array instead
Solutions¶
Return an empty array
[]instead ofnullfrom__debugInfo().Remove the
__debugInfo()method entirely if you want the defaultvar_dump()behavior.Return the array of properties you want to display:
return ['key' => $this->value];.
See Also¶
Changed Behavior¶
This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.