note 77559 deleted from language.oop5.decon by nlopess
| From: | nlopess@php.net | Date: | Tue, 04 Sep 2007 10:01:18 +0000 |
| Subject: | note 77559 deleted from language.oop5.decon by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-130150@lists.php.net to get a copy of this message | ||
Note Submitter: bishop (php at ideacode dot com)
Reason: spam
----
You can pass all arguments from a derived class's constructor to the parent's constructor
using func_get_args(), call_user_func_array(), and parent. For example, say you wanted to add
logging to a class, but let the base do all the usual work:
<?php
class InstrumentedPDO extends PDO {
private $log = null;
public function __construct() {
// connect to logging
require_once('Log.php');
$this->log = Log::factory('display');
// pass through
$args = func_get_args();
call_user_func_array(array (parent, '__construct'), $args);
}
public function query() {
$args = func_get_args();
// log it
if (isset($this->log)) {
$this->log->log("PDO query=[{$args[0]}]");
}
// pass through
return call_user_func_array(array (parent, 'query'), $args);
}
}
?>
http://bytejar.com/ - Software Lessons Learned the Hard Way