note 86622 deleted from language.oop5.magic by cmb
| From: | cmb@php.net | Date: | Sun, 10 Nov 2019 12:36:55 +0000 |
| Subject: | note 86622 deleted from language.oop5.magic by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212982@lists.php.net to get a copy of this message | ||
Note Submitter: patricknegri at gmail dot com
----
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);
?>