note 77508 deleted from language.oop5.overloading by cmb
| From: | cmb@php.net | Date: | Sun, 03 Mar 2019 11:36:15 +0000 |
| Subject: | note 77508 deleted from language.oop5.overloading by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210308@lists.php.net to get a copy of this message | ||
Note Submitter: ac221 at sussex dot ac dot uk
----
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 :)