note 60009 added to language.oop5.overloading
| From: | PHPatjyoppdotKomm at osu1 dot php dot net | Date: | Thu, 22 Dec 2005 19:01:45 +0000 |
| Subject: | note 60009 added to language.oop5.overloading | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-100590@lists.php.net to get a copy of this message | ||
Here's a useful class for logging function calls. It stores a sequence of calls and arguments
which can then be applied to objects later. This can be used to script common sequences of
operations, or to make "pluggable" operation sequences in header files that can be
replayed on objects later.
If it is instantiated with an object to shadow, it behaves as a mediator and executes the calls on
this object as they come in, passing back the values from the execution.
This is a very general implementation; it should be changed if error codes or exceptions need to be
handled during the Replay process.
<?php
class MethodCallLog {
private $callLog = array();
private $object;
public function __construct($object = null) {
$this->object = $object;
}
public function __call($m, $a) {
$this->callLog[] = array($m, $a);
if ($this->object) return call_user_func_array(array(&$this->object,$m),$a);
return true;
}
public function Replay(&$object) {
foreach ($this->callLog as $c) {
call_user_func_array(array(&$object,$c[0]), $c[1]);
}
}
public function GetEntries() {
$rVal = array();
foreach ($this->callLog as $c) {
$rVal[] = "$c[0](".implode(', ', $c[1]).");";
}
return $rVal;
}
public function Clear() {
$this->callLog = array();
}
}
$log = new MethodCallLog();
$log->Method1();
$log->Method2("Value");
$log->Method1($a, $b, $c);
// Execute these method calls on a set of objects...
foreach ($array as $o) $log->Replay($o);
?>
----
Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60009
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60009&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60009&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60009&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60009&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60009&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60009&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60009&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60009&report=yes
Search -- http://master.php.net/manage/user-notes.php