note 77560 added to function.call-user-func-array

From: Date: Tue, 04 Sep 2007 00:07:07 +0000
Subject: note 77560 added to function.call-user-func-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-130142@lists.php.net to get a copy of this message
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 ---- Server IP: 64.71.164.2 Probable Submitter: 24.148.133.105 ---- Manual Page -- http://www.php.net/manual/en/function.call-user-func-array.php Edit -- https://master.php.net/note/edit/77560 Del: integrated -- https://master.php.net/note/delete/77560/integrated Del: useless -- https://master.php.net/note/delete/77560/useless Del: bad code -- https://master.php.net/note/delete/77560/bad+code Del: spam -- https://master.php.net/note/delete/77560/spam Del: non-english -- https://master.php.net/note/delete/77560/non-english Del: in docs -- https://master.php.net/note/delete/77560/in+docs Del: other reasons-- https://master.php.net/note/delete/77560 Reject -- https://master.php.net/note/reject/77560 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#130142) next »