note 84631 added to function.call-user-func-array
| From: | thiago dot henrique dot mata at gmail dot com | Date: | Tue, 22 Jul 2008 21:17:16 +0000 |
| Subject: | note 84631 added to function.call-user-func-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-142574@lists.php.net to get a copy of this message | ||
<?php
Class Delegate
{
private $arrInstances = array();
protected function addObject( $oElement )
{
// add one element on the end of the stack //
$this->arrInstances[] = $oElement;
}
public function __call( $strMethod, $arrParams )
{
// for each element in instance //
foreach( $this->arrInstances as $oElement )
{
// get the class of the element //
$strClass = get_class( $oElement );
// get all methods of the class //
$arrMethods = get_class_methods( $strClass );
// case the method exists into this class //
if( in_array( $strMethod , $arrMethods ) )
{
// prepare caller //
$arrCaller = Array( $strClass , $strMethod );
// return the result of the method into the object //
return call_user_func_array( $arrCaller, $arrParams );
}
}
// any object has the method //
// throw a exception //
throw new Exception( " Method " . $strMethod . " not exist in this class " .
get_class( $this ) . "." );
}
}
class Log
{
public function sayHi()
{
print "hi!" . "<br/>\n";
}
public function sayMyName()
{
print "log" . "<br/>\n";
}
}
class Other
{
public function sayHello()
{
print "hello there!" . "<br/>\n";
}
public function sayMyName()
{
print "other" . "<br/>\n";
}
}
class Example extends Delegate
{
public function __construct()
{
$this->addObject( new Log() );
$this->addObject( new Other() );
}
}
$oExample = new Example();
$oExample->sayHi();
$oExample->sayHello();
$oExample->sayMyName();
/*
hi!<br/>
hello there!<br/>
log<br/>
*/
?>
----
Server IP: 208.69.120.35
Probable Submitter: 69.10.36.3
----
Manual Page -- http://www.php.net/manual/en/function.call-user-func-array.php
Edit -- https://master.php.net/note/edit/84631
Del: integrated -- https://master.php.net/note/delete/84631/integrated
Del: useless -- https://master.php.net/note/delete/84631/useless
Del: bad code -- https://master.php.net/note/delete/84631/bad+code
Del: spam -- https://master.php.net/note/delete/84631/spam
Del: non-english -- https://master.php.net/note/delete/84631/non-english
Del: in docs -- https://master.php.net/note/delete/84631/in+docs
Del: other reasons-- https://master.php.net/note/delete/84631
Reject -- https://master.php.net/note/reject/84631
Search -- https://master.php.net/manage/user-notes.php