note 98665 added to keyword.extends
| From: | f4bi0_ at hotmail dot com | Date: | Tue, 29 Jun 2010 18:19:47 +0000 |
| Subject: | note 98665 added to keyword.extends | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170561@lists.php.net to get a copy of this message | ||
<?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);
}
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;
?>
don't hesitate to email me, happy coding! ;)
----
Server IP: 69.147.83.197
Probable Submitter: 187.53.204.38
----
Manual Page -- http://www.php.net/manual/en/keyword.extends.php
Edit -- https://master.php.net/note/edit/98665
Del: integrated -- https://master.php.net/note/delete/98665/integrated
Del: useless -- https://master.php.net/note/delete/98665/useless
Del: bad code -- https://master.php.net/note/delete/98665/bad+code
Del: spam -- https://master.php.net/note/delete/98665/spam
Del: non-english -- https://master.php.net/note/delete/98665/non-english
Del: in docs -- https://master.php.net/note/delete/98665/in+docs
Del: other reasons-- https://master.php.net/note/delete/98665
Reject -- https://master.php.net/note/reject/98665
Search -- https://master.php.net/manage/user-notes.php