note 82273 added to language.oop
| From: | simshaun at gmail dot com | Date: | Thu, 03 Apr 2008 15:55:55 +0000 |
| Subject: | note 82273 added to language.oop | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-137895@lists.php.net to get a copy of this message | ||
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
----
Server IP: 209.41.74.194
Probable Submitter: 71.48.195.224
----
Manual Page -- http://www.php.net/manual/en/language.oop.php
Edit -- https://master.php.net/note/edit/82273
Del: integrated -- https://master.php.net/note/delete/82273/integrated
Del: useless -- https://master.php.net/note/delete/82273/useless
Del: bad code -- https://master.php.net/note/delete/82273/bad+code
Del: spam -- https://master.php.net/note/delete/82273/spam
Del: non-english -- https://master.php.net/note/delete/82273/non-english
Del: in docs -- https://master.php.net/note/delete/82273/in+docs
Del: other reasons-- https://master.php.net/note/delete/82273
Reject -- https://master.php.net/note/reject/82273
Search -- https://master.php.net/manage/user-notes.php