<?php
/**
* Abomination class implementing multiple inheritance via
* __call magic method
*
*//------------------------object class--------------------------------------------------
abstract class MultipleInheritance
{
/**
* List of parent classes
*
* @var array
* @access protected
*/
protected $_parents = array();
/**
* List of parent objects - generated automatically
*
* @var array
* @access private
*/
private $_parentObjects = array();
/**
* Constructor (thank you captain Obvious)
*
* - init the parent objects
*
* @access public
*/
public function __construct()
{
foreach ($this->_parents as $parentClass) {
$this->_parentObjects []= new $parentClass();
}
}
/**
* __call magic method
*
* @param string $name
* @param array $arguments
* @access public
* @return mixed
*/
public function __call($name, array $arguments)
{
foreach ($this->_parentObjects as $object) {
if (method_exists($object, $name)) {
return call_user_func_array(array($object, $name), $arguments);
}
}
throw new Exception('No such method: ' . $name);
}
}
//--------------------------------------------------------------------------
class first
{
public function firstmethod()
{
echo 'first method call<br>' ;
}
}
//--------------------second------------------------------------------------------
class second
{
public function sendmethod()
{
echo 'second method call';
}
}
//----------------------mainclass----------------------------------------------------
class mainclass extends MultipleInheritance
{
protected $_parents = array('first', 'second');
}
$child = new mainclass();
$child->firstmethod();
$child->sendmethod();
/**
* Abomination class implementing multiple inheritance via
* __call magic method
*
*//------------------------object class--------------------------------------------------
abstract class MultipleInheritance
{
/**
* List of parent classes
*
* @var array
* @access protected
*/
protected $_parents = array();
/**
* List of parent objects - generated automatically
*
* @var array
* @access private
*/
private $_parentObjects = array();
/**
* Constructor (thank you captain Obvious)
*
* - init the parent objects
*
* @access public
*/
public function __construct()
{
foreach ($this->_parents as $parentClass) {
$this->_parentObjects []= new $parentClass();
}
}
/**
* __call magic method
*
* @param string $name
* @param array $arguments
* @access public
* @return mixed
*/
public function __call($name, array $arguments)
{
foreach ($this->_parentObjects as $object) {
if (method_exists($object, $name)) {
return call_user_func_array(array($object, $name), $arguments);
}
}
throw new Exception('No such method: ' . $name);
}
}
//--------------------------------------------------------------------------
class first
{
public function firstmethod()
{
echo 'first method call<br>' ;
}
}
//--------------------second------------------------------------------------------
class second
{
public function sendmethod()
{
echo 'second method call';
}
}
//----------------------mainclass----------------------------------------------------
class mainclass extends MultipleInheritance
{
protected $_parents = array('first', 'second');
}
$child = new mainclass();
$child->firstmethod();
$child->sendmethod();
No comments:
Post a Comment