note 101751 added to keyword.extends

From: Date: Sun, 09 Jan 2011 04:33:04 +0000
Subject: note 101751 added to keyword.extends
Groups: php.notes 
Request: Send a blank email to php-notes+get-175554@lists.php.net to get a copy of this message
// I've modified the snippet from f4bi0_ to fit my own needs // Now you can do something like "$Ext2 = new $AL->Ext2;" // (It's not fully tested out yet...) <?php if (!class_exists("AgiLab")) { class AgiLab { private $childs = array(); public function add($child) { if (gettype($child) == "object") $this->childs[get_class($child)] = $child; else if (gettype($child) == "string") $this->childs[$child] = $child; } public function __get($varname) { if (isset($this->childs[$varname])) return $this->childs[$varname]; foreach ($this->childs as $child) if (gettype($child) == "object") if (property_exists($child, $varname)) return $child->$varname; } public function __call($method, $args) { foreach ($this->childs as $child) if (gettype($child) == "object") if (method_exists($child, $method)) return call_user_method_array($method, $child, $args); } } $AL = new AgiLab(); } /********************/ class Ext1{ private $name=""; private $id=""; public function setID($id){$this->id = $id;} public function setName($name){$this->name = $name;} public function getID(){return $this->id;} public function getName(){return $this->name;} } $AL->add(new Ext1()); /********************/ class Ext2{ private $address=""; private $country=""; public function setAddress($address){$this->address = $address;} public function setCountry($country){$this->country = $country;} public function getAddress(){return $this->address;} public function getCountry(){return $this->country;} } $AL->add(Ext2); /********************/ $AL->setName("Agita"); echo $AL->getName()."<br>"; $AL->setCountry("Germany"); echo $AL->getCountry()."<br>"; // doesnt work $Ext2 = new $AL->Ext2; $Ext2->setAddress("Hannover"); echo $Ext2->getAddress()."<br>"; ---- Server IP: 69.147.83.197 Probable Submitter: 77.189.87.59 ---- Manual Page -- http://www.php.net/manual/en/keyword.extends.php Edit -- https://master.php.net/note/edit/101751 Del: integrated -- https://master.php.net/note/delete/101751/integrated Del: useless -- https://master.php.net/note/delete/101751/useless Del: bad code -- https://master.php.net/note/delete/101751/bad+code Del: spam -- https://master.php.net/note/delete/101751/spam Del: non-english -- https://master.php.net/note/delete/101751/non-english Del: in docs -- https://master.php.net/note/delete/101751/in+docs Del: other reasons-- https://master.php.net/note/delete/101751 Reject -- https://master.php.net/note/reject/101751 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#175554) next »