note 82273 deleted from language.oop by danbrown
| From: | danbrown@php.net | Date: | Sat, 07 Nov 2009 19:02:35 +0000 |
| Subject: | note 82273 deleted from language.oop by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162551@lists.php.net to get a copy of this message | ||
Note Submitter: simshaun at gmail dot com
----
This may be of help to some. I was looking for a way to have a wrapper class with subclasses.
From the subclasses, I wanted to be able to call functions (and even pass arguments to those
functions) from other subclasses, using the wrapper class as a bridge.
My solution:
---------------------
class parser
{
var $class;
function parser()
{
$this->class = array();
}
function LoadClass($className)
{
$this->class[$className] = new $className;
}
function RunFunction($className, $functionName, $functionArgs=array() )
{
call_user_func_array( array( &$this->class[$className], $functionName) , $functionArgs);
}
function GetVarValue($className, $varName)
{
return $this->class[$className][$varName];
}
}
class test
{
var $myvar;
var $name;
function test()
{
$this->myvar = 1;
$this->name = "Shaun";
}
function runme($name=null)
{
if ($name !== null)
{
echo "Hi " . $name;
}
else
{
echo "Hi " . $this->name;
}
}
}
class test2 extends parser
{
function runme()
{
parent::LoadClass("test");
parent::RunFunction("test", "runme", array("name" =>
"Bob") );
}
}
Usage:
-----------
$parser = new parser;
$test = new test;
$test2 = new test2;
$test2->runme(); // outputs Bob