note 76622 added to language.oop5.overloading

From: Date: Mon, 23 Jul 2007 14:23:43 +0000
Subject: note 76622 added to language.oop5.overloading
Groups: php.notes 
Request: Send a blank email to php-notes+get-128843@lists.php.net to get a copy of this message
Just to reinforce and elaborate on what DevilDude at darkmaker dot com said way down there on 22-Sep-2004 07:57. The recursion detection feature can prove especially perilous when using __set. When PHP comes across a statement that would usually call __set but would lead to recursion, rather than firing off a warning or simply not executing the statement it will act as though there is no __set method defined at all. The default behaviour in this instance is to dynamically add the specified property to the object thus breaking the desired functionality of all further calls to __set or __get for that property. Example: <?php class TestClass{ public $values = array(); public function __get($name){ return $this->values[$name]; } public function __set($name, $value){ $this->values[$name] = $value; $this->validate($name); } public function validate($name){ /* __get will be called on the following line but as soon as we attempt to call __set again PHP will refuse and simply add a property called $name to $this */ $this->$name = trim($this->$name); } } $tc = new TestClass(); $tc->foo = 'bar'; $tc->values['foo'] = 'boing'; echo '$tc->foo == ' . $tc->foo . '<br>'; echo '$tc ' . (property_exists($tc, 'foo') ? 'now has' : 'still does not have') . ' a property called "foo"<br>'; /* OUPUTS: $tc->foo == bar $tc now has a property called "foo" */ ?> ---- Server IP: 194.145.210.4 Probable Submitter: 86.17.149.150 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php Edit -- https://master.php.net/note/edit/76622 Del: integrated -- https://master.php.net/note/delete/76622/integrated Del: useless -- https://master.php.net/note/delete/76622/useless Del: bad code -- https://master.php.net/note/delete/76622/bad+code Del: spam -- https://master.php.net/note/delete/76622/spam Del: non-english -- https://master.php.net/note/delete/76622/non-english Del: in docs -- https://master.php.net/note/delete/76622/in+docs Del: other reasons-- https://master.php.net/note/delete/76622 Reject -- https://master.php.net/note/reject/76622 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128843) next »