unsetting class properties

php.internals

(Marcus Börger)

23 years ago
Hello Zeev, hi Andi, atm it is not possible to unset static members of a class which is correct since that would modify the class protocol. But it is possible to unset non dynamic properties (defined in the class). This should also be prevented as done with the attached patch. I did it by checking whether the name is part of the default properties. This works but maybe it is to slow. So perhaps it is a better solution to provide a flag in the property_info but then we#d have to do that in every class instantiation while my method would only take time when unset is used on props. Test code: <?php class u { var $x = 1; } class t extends u {} $o = new t; $o->y=2; $o->var_dump($o); $o->unset($o->y); $o->var_dump($o); $o->unset($o->x); $o->var_dump($o);' ?> Result: object(t)#1 (2) { ["x"]=> int(1) ["y"]=> int(2) } object(t)#1 (0) { } With Patch: object(t)#1 (2) { ["x"]=> int(1) ["y"]=> int(2) } object(t)#1 (1) { ["x"]=> int(1) } Comments ? Best regards, Marcus mailto:helly@php.net

(Marcus Börger)

23 years ago
Hello Marcus, Saturday, July 5, 2003, 4:16:03 PM, you wrote: MB> Hello Zeev, hi Andi, MB> atm it is not possible to unset static members of a class which is correct MB> since that would modify the class protocol. But it is possible to unset non MB> dynamic properties (defined in the class). This should also be prevented as MB> done with the attached patch. MB> I did it by checking whether the name is part of the default properties. This MB> works but maybe it is to slow. So perhaps it is a better solution to provide a MB> flag in the property_info but then we#d have to do that in every class MB> instantiation while my method would only take time when unset is used on MB> props. MB> Test code: MB> <?php MB> class u { var $x = 1; } MB> class t extends u {} MB> $o = new t; $o->>y=2; $o->>var_dump($o); $o->>unset($o->y); $o->>var_dump($o); $o->>unset($o->x); $o->>var_dump($o);' ?>> MB> Result: MB> object(t)#1 (2) { MB> ["x"]=> MB> int(1) MB> ["y"]=> MB> int(2) MB> } MB> object(t)#1 (0) { MB> } MB> With Patch: MB> object(t)#1 (2) { MB> ["x"]=> MB> int(1) MB> ["y"]=> MB> int(2) MB> } MB> object(t)#1 (1) { MB> ["x"]=> MB> int(1) MB> } MB> Comments ? During a nice chat with Andi both of us came to the conclusion that the user may shoot himself in the knee if he wants to. So we allow unsetting default properties what makes PHP objects a thing between real objects and pure instances. Best regards, Marcus mailto:helly@php.net

Zeev Suraski

23 years ago
At 18:36 05/07/2003, Marcus Börger wrote:
>During a nice chat with Andi both of us came to the conclusion that the user >may shoot himself in the knee if he wants to. So we allow unsetting default >properties what makes PHP objects a thing between real objects and pure >instances.
Just for the record, the pain involved in this shot won't be that significant. Because of the way the engine works, the access information related to the property (PPP setting, whether it's static or not, etc.) are kept separately, and won't be effected by unset(). So, if you unset a private property, it still remains private, just without any value currently attached to it. It's really not that bad. Zeev