note 115095 added to language.oop5.overloading

From: Date: Sat, 24 May 2014 13:40:58 +0000
Subject: note 115095 added to language.oop5.overloading
Groups: php.notes 
Request: Send a blank email to php-notes+get-199614@lists.php.net to get a copy of this message
If you need to also use the magic setter in your constructor, you may use this example: <?php class person { private $name; private $addr; private $city; function __construct($n,$a,$c) { $this->__set('name', $n); // Pushing "name" through the setter where it will be validated and sanitated. $this->addr = $a; // Adress is not validated $this->city = $c; // Neither is this... // $this->__set('city',$c); // But this is... } public function __get($property) {if (property_exists($this, $property)) {return $this->$property;}} public function __set($property, $value) { if (property_exists($this, $property)) { echo "<pre>Setting $value</pre>"; // ... Clever code for sanitation... if ($value == "Evil input from user"){ echo "<pre>Oh no! Bad user! Go sit in the corner!</pre>"; $this->$property = null; } else { $this->$property = $value; } } return $this; } } $cathy = new person('Cathy','9 Dark and Twisty','Evil input from user'); $cathy->name = "Kitty Kat"; echo "<pre>" . print_r($cathy,1) . "</pre>"; echo "<pre>" . $cathy->city . "</pre>"; ?> This code will output: Setting Kitty Kat person Object ( [name:person:private] => Kitty Kat [addr:person:private] => 9 Dark and Twisty [city:person:private] => Evil input from user ) Evil input from user So it is important to push variables from the user through the setters even in the constructor. ---- Server IP: 72.52.91.14 Probable Submitter: 84.202.211.71 ---- Manual Page -- http://php.net/manual/en/language.oop5.overloading.php Edit -- https://master.php.net/note/edit/115095 Del: integrated -- https://master.php.net/note/delete/115095/integrated Del: useless -- https://master.php.net/note/delete/115095/useless Del: bad code -- https://master.php.net/note/delete/115095/bad+code Del: spam -- https://master.php.net/note/delete/115095/spam Del: non-english -- https://master.php.net/note/delete/115095/non-english Del: in docs -- https://master.php.net/note/delete/115095/in+docs Del: other reasons-- https://master.php.net/note/delete/115095 Reject -- https://master.php.net/note/reject/115095 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#199614) next »