Skip to content

no-unset-hookable-property

PHPsuspiciousno fixdefault: offphp/phpstan

Reports unset($this->name) of a property the class declares when a subclass could give the property a hook (PHP 8.4): the class is not final and the property is not private, final, static or readonly.

A property with a hook cannot be unset, so the call fails with an error in the subclass that overrides the property, while it works in the class that wrote it.

To fix, set the property to null instead, or make the class or the property final.

Reported
<?php
class A { public $a; private $b; public readonly int $c;
function f() { unset($this->a); unset($this->b); unset($this->c); unset($this->z); }
}
final class B { public $a; function f() { unset($this->a); } }
php:
lint:
rules:
no-unset-hookable-property: warn # off (default), warn, error

Enabled by the presets: php/phpstan.

Suppress one line with sudsy-disable-next-line no-unset-hookable-property in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
phpstan unset.possiblyHookedProperty partial unset($this->p) of a property the class declares; a property declared in a parent in another file is not seen.