The function unset()
is for array elements, variables and object attributes.
It is not possible to exclude a property from a class declared in the scope of the class, regardless of the declaration type or data type.
For static properties is emitted as fatal error because static variables are allocated in spaces stacking values such 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 this does not mean that the class will be excluded.
$a = new stdClass();
unset($a);
class Bar{}
$a = new Bar();
unset($a);
For properties not declared as static, a fatal error is not issued, as 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.
But what about the class issue? I’d like to understand why when we use in a class property it generates
Fatal Erro
!– Wallace Maxters
an important note, such execution may enter as deprecated or completely abolished in the future. Currently issues "notice Strict" because it has no explicit statement of the class visibility type and its members (properties and methods)
– Daniel Omine