note 77508 added to language.oop5.overloading
| From: | ac221 at sussex dot ac dot uk | Date: | Sat, 01 Sep 2007 17:07:04 +0000 |
| Subject: | note 77508 added to language.oop5.overloading | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130067@lists.php.net to get a copy of this message | ||
Just Noting the interesting behavior of __set __get , when modifying objects contained in overloaded
properties.
<?php
class foo {
public $propObj;
public function __construct(){
$propObj = new stdClass();
}
public function __get($prop){
echo("I'm Being Got ! \n");
return $this->propObj->$prop;
}
public function __set($prop,$val){
echo("I'm Being Set ! \n");
$this->propObj->$prop = $val;
}
}
$test = new foo();
$test->barProp = new stdClass(); // I should invoke set
$test->barProp->barSubProp = 'As Should I';
$test->barProp->barSubProp = 'As Should I';
$test->barProp = new stdClass(); // As should i
?>
Outputs:
I'm Being Set !
I'm Being Got !
I'm Being Got !
I'm Being Set !
Whats happening, is PHP is acquiring a reference to the object, triggering __get; Then applying the
changes to the object via the reference.
Which is the correct behaviour; objects being special creatures, with an aversion to being cloned...
Unfortunately this will never invoke the __set handler, even though it is modifying a property
within 'foo', which is slightly annoying if you wanted to keep track of changes to an
objects overloaded properties.
I guess Journaled Objects will have to wait till PHP 6 :)
----
Server IP: 87.124.64.96
Probable Submitter: 139.184.30.16 (proxied: 139.184.222.41)
----
Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php
Edit -- https://master.php.net/note/edit/77508
Del: integrated -- https://master.php.net/note/delete/77508/integrated
Del: useless -- https://master.php.net/note/delete/77508/useless
Del: bad code -- https://master.php.net/note/delete/77508/bad+code
Del: spam -- https://master.php.net/note/delete/77508/spam
Del: non-english -- https://master.php.net/note/delete/77508/non-english
Del: in docs -- https://master.php.net/note/delete/77508/in+docs
Del: other reasons-- https://master.php.net/note/delete/77508
Reject -- https://master.php.net/note/reject/77508
Search -- https://master.php.net/manage/user-notes.php