note 86622 modified in language.oop5.magic by danbrown
| From: | danbrown@php.net | Date: | Mon, 27 Oct 2008 14:33:01 +0000 |
| Subject: | note 86622 modified in language.oop5.magic by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-145834@lists.php.net to get a copy of this message | ||
Imports Pattern - Extend Classes in Real Time:
<?php
class BaseClass
{
var $__imported;
var $__imported_functions;
function __construct()
{
$__imported = Array();
$__imported_functions = Array();
}
function Imports($object)
{
$new_imports = new $object();
$imports_name = get_class($new_imports);
array_push( $__imported, Array($imports_name,$new_imports) );
$imports_function = get_class_methods($new_imports);
foreach ($imports_function as $i=>$function_name)
{
$this->__imported_functions[$function_name] = &$new_imports;
}
}
function __call($m, $a)
{
if (array_key_exists($m,$this->__imported_functions))
{
return call_user_func_array(Array($this->__imported_functions[$m],$m),$a);
}
throw new ErrorException ('Call to Undefined Method/Class Function', 0, E_ERROR);
}
}
class ExternalFunc
{
function TestB()
{
echo "External Imported!";
}
}
class B extends BaseClass
{
function __construct()
{
$this->Imports("ExternalFunc");
}
function Msg()
{
echo "Hello world<br />";
}
}
$b = new B();
$b->Msg();
// or call $b->Imports("ExternalFunc");
$b->TestB();
//$b->TestB(1,3,4);
?>
--was--
Imports Pattern - Extend Classes in Real Time:
class BaseClass
{
var $__imported;
var $__imported_functions;
function __construct()
{
$__imported = Array();
$__imported_functions = Array();
}
function Imports($object)
{
$new_imports = new $object();
$imports_name = get_class($new_imports);
array_push( $__imported, Array($imports_name,$new_imports) );
$imports_function = get_class_methods($new_imports);
foreach ($imports_function as $i=>$function_name)
{
$this->__imported_functions[$function_name] = &$new_imports;
}
}
function __call($m, $a)
{
if (array_key_exists($m,$this->__imported_functions))
{
return call_user_func_array(Array($this->__imported_functions[$m],$m),$a);
}
throw new ErrorException ('Call to Undefined Method/Class Function', 0, E_ERROR);
}
}
class ExternalFunc
{
function TestB()
{
echo "External Imported!";
}
}
class B extends BaseClass
{
function __construct()
{
$this->Imports("ExternalFunc");
}
function Msg()
{
echo "Hello world<br />";
}
}
$b = new B();
$b->Msg();
// or call $b->Imports("ExternalFunc");
$b->TestB();
//$b->TestB(1,3,4);
http://php.net/manual/en/language.oop5.magic.php