Unsetting Properties Surprises¶
<?php
class X {
public $a;
public ?int $b = 1;
function clean() {
unset($this->a);
unset($this->b);
}
}
$x = new X;
$x->clean();
var_dump($x->a);
var_dump($x->b);
?>
Unsetting properties is always a surprise.
First, if the property was typed, it yields a Fatal Error, as the property cannot be accessed before initialization. And, the unset destroyed the property.
Also, checking an unset property with property_exists() is done against the class definition, not the current object state.
See Also¶
Unset Properties [Try me]
PHP Error Messages¶
PHP Features¶
Last updated: 14 July 2026