note 109843 deleted from keyword.extends by danbrown
| From: | danbrown@php.net | Date: | Fri, 31 Aug 2012 15:40:29 +0000 |
| Subject: | note 109843 deleted from keyword.extends by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190316@lists.php.net to get a copy of this message | ||
Note Submitter: shashank at neupane dot com dot np
----
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;
?>