The function unset()
is for elements of arrays, variables, and object attributes.
You can not delete a property of a class declared in the scope of the class, regardless of the type of declaration or type of data.
For static properties it is issued as fatal error because static variables are allocated in value stacking spaces as reference links.
Ok, we know what arrays and variables are, however, what would be "object attributes"?
Example:
$a = new stdClass();
$a->foo = 'test';
$var_dump($a);
/*
object(stdClass)#1 (1) {
["foo"]=>
string(4) "test"
}
*/
unset($a->foo);
var_dump($a);
/*
object(stdClass)#1 (0) {
}
*/
Note that an entire object can also be deleted when assigned as an instance. But that does not mean that the class will be deleted.
$a = new stdClass();
unset($a);
class Bar{}
$a = new Bar();
unset($a);
For properties not declared as static, a fatal error is not issued, because the property, when public, becomes a member of the new instance of the object and not of the original class itself:
class Foo {
public $bar = 'bar';
}
$obj = new Foo;
print_r($obj);
unset($obj->bar); // Aqui excluímos a propriedade
print_r($obj); // Vejamos se realmente foi exluída
$obj = new Foo;
print_r($obj); // Veja o que acontece se, logo em seguida criamos uma nova instância. A propriedade original permanece inalterada -pois trata-se de uma nova instância.