Error Identifier: unset.possiblyHookedProperty
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
class User
{
public string $name;
public function reset(): void
{
unset($this->name);
}
}
Why is it reported? #
Starting with PHP 8.4, properties can have hooks (get/set). Unsetting a property that is not private, not final, and belongs to a non-final class is reported because a subclass could add hooks to that property. Unsetting a hooked property is not allowed in PHP, so the unset() call could fail at runtime if a child class introduces hooks.
How to fix it #
Mark the property or class as final to prevent subclasses from adding hooks, or mark the property as private.
<?php declare(strict_types = 1);
class User
{
- public string $name;
+ public final string $name;
public function reset(): void
{
unset($this->name);
}
}
Alternatively, mark the entire class as final:
<?php declare(strict_types = 1);
-class User
+final class User
{
public string $name;
public function reset(): void
{
unset($this->name);
}
}
How to ignore this error #
You can use the identifier unset.possiblyHookedProperty to ignore this error using a comment:
// @phpstan-ignore unset.possiblyHookedProperty
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: unset.possiblyHookedProperty
Rules that report this error #
- PHPStan\Rules\Variables\UnsetRule [1]