note 77560 deleted from function.call-user-func-array by nlopess
| From: | nlopess@php.net | Date: | Tue, 04 Sep 2007 10:01:30 +0000 |
| Subject: | note 77560 deleted from function.call-user-func-array by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-130151@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);
}
}
?>
Earlier posts have shown using a callback of array ($this, 'parent::__construct'), but as
of PHP 5.2.4, that can be simplified as above. Note that parent is *not* quoted in the
constructor's call_user_func_array() call.
http://bytejar.com/ - Software Lessons Learned the Hard Way