note 54449 added to function.unset

From: Date: Tue, 05 Jul 2005 02:08:20 +0000
Subject: note 54449 added to function.unset
Groups: php.notes 
Request: Send a blank email to php-notes+get-91448@lists.php.net to get a copy of this message
We have experienced when we applied 'unset' to the overloaded properties (PHP5), consider the code below: <?php class TheObj { public $RealVar1, $RealVar2, $RealVar3, $RealVar4; public $Var = array(); function __set($var, $val) { $this->Var[$var] = $val; } function __get($var) { if(isset($this->Var[$var])) return $this->Var[$var]; else return -1; } } $SomeObj = new TheObj; // here we set for real variables $SomeObj->RealVar1 = 'somevalue'; $SomeObj->{'RealVar2'} = 'othervalue'; $SomeObj->{'RealVar'.(3)} = 'othervaluetoo'; $SomeObj->{'RealVar'.'4'} = 'anothervalue'; // and here we set for virtual variables $SomeObj->Virtual1 = 'somevalue'; $SomeObj->{'Virtual2'} = 'othervalue'; $SomeObj->{'Virtual'.(3)} = 'othervaluetoo'; $SomeObj->{'Virtual'.'4'} = 'anothervalue'; // now we will try to unset these variables unset($SomeObj->RealVar1); unset($SomeObj->{'RealVar'.(3)}); //the lines below will catch by '__get' magic method since these variables are unavailable anymore print $SomeObj->RealVar1."\n"; print $SomeObj->{'RealVar'.(3)}."\n"; // now we will try to unset these variables unset($SomeObj->Virtual1); unset($SomeObj->{'Virtual'.(3)}); //but, these variables are still available??? eventhough they're "unset"-ed print $SomeObj->Virtual1."\n"; print $SomeObj->{'Virtual'.(3)}."\n"; ?> Please note that PHP doesn't have magic callback to unset overloaded properties. This is the reason why unset($SomeObj->Virtual1) doesn't work. But it does work when we set 'null' value such as the following code: <?php // now we will set 'null' value instead of using unset statement $SomeObj->Virtual1 = null; $SomeObj->{'Virtual'.(3)} = null; // and now these variables are no longer available print $SomeObj->Virtual1."\n"; print $SomeObj->{'Virtual'.(3)}."\n"; ?> Sound ugly, yeah? This applied to the "virtual" array variable too, see more at http://bugs.php.net/bug.php?id=33513 (at feedback) about it. PS: we used PHP version 5.1.0-dev from the CVS snapshot when we wrote the above codes. ---- Manual Page -- http://www.php.net/manual/en/function.unset.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54449 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54449&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54449&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54449&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54449&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54449&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54449&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54449&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#91448) next »