note 109843 added to keyword.extends
| From: | shashank at neupane dot com dot np | Date: | Mon, 27 Aug 2012 06:25:36 +0000 |
| Subject: | note 109843 added to keyword.extends | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190232@lists.php.net to get a copy of this message | ||
little modification to the code provided by f4bio__ for extending more than one class as the
function used by him are deprecated by now.
<?php
// what if we want to extend more then one class?
Abstract class ExtensionBridge
{
// array containing all the extended classes
private $_exts = array();
public $_this;
function __construct(){$_this = $this;}
public function addExt($object)
{
$this->_exts[]=$object;
}
public function __get($varname)
{
foreach($this->_exts as $ext)
{
if(property_exists($ext,$varname))
return $ext->$varname;
}
}
public function __call($method,$args)
{
foreach($this->_exts as $ext)
{
if(method_exists($ext,$method))
// return call_user_method_array($method,$ext,$args);
return call_user_func_array(array($ext, $method), $args);
}
throw new Exception("This Method {$method} doesn't exists");
}
}
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;}
}
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;}
}
class Extender extends ExtensionBridge
{
function __construct()
{
parent::addExt(new Ext1());
parent::addExt(new Ext2());
}
public function __toString()
{
return $this->getName().', from: '.$this->getCountry();
}
}
$o = new Extender();
$o->setName("fabio");
$o->setCountry("brazil");
echo $o;
?>
----
Server IP: 69.147.83.197
Probable Submitter: 49.244.102.171
----
Manual Page -- http://www.php.net/manual/en/keyword.extends.php
Edit -- https://master.php.net/note/edit/109843
Del: integrated -- https://master.php.net/note/delete/109843/integrated
Del: useless -- https://master.php.net/note/delete/109843/useless
Del: bad code -- https://master.php.net/note/delete/109843/bad+code
Del: spam -- https://master.php.net/note/delete/109843/spam
Del: non-english -- https://master.php.net/note/delete/109843/non-english
Del: in docs -- https://master.php.net/note/delete/109843/in+docs
Del: other reasons-- https://master.php.net/note/delete/109843
Reject -- https://master.php.net/note/reject/109843
Search -- https://master.php.net/manage/user-notes.php