Home » Database » MDB2 » Bug #3464
__call overload to call loaded modules' functions
Details
| Request #3464 | __call overload to call loaded modules' functions |
|---|---|
| Submitted | 2005-02-14 17:06 UTC |
| From | ceefour at gauldong dot net |
| Status | Closed |
| Package | MDB2 |
| PHP Version | 5.0.3 |
| OS | Linux, Apache 1.3.33 |
| Roadmaps | (Not assigned) |
Comments
[2005-02-14 17:06 UTC] ceefour at gauldong dot net
Description:
------------
This works in PHP 5.0.3, it has no effect on PHP < 5.0.
BTW about module functions overloading...
This works for me (modified MDB2_Driver_Common):
/**
* Calls a module method (currently supports Extended and Extended2).
*
* @param string Method name.
* @param array Arguments.
* @return mixed Returned value.
*/
function __call($name, $args) {
if (isset($this->extended) && method_exists($this->extended, $name)) {
return call_user_func_array(array($this->extended, $name), $args);
} else if (isset($this->extended2) && method_exists($this->extended2,
$name)) {
return call_user_func_array(array($this->extended2, $name), $args);
} else throw new Exception("Method $name does not exists."); // whoops!
Exceptions! ;-)
}
BTW you'll quickly notice that the module names were hardcoded, since
there's no way to determine which modules are loaded (traversing through
the property list will suffer performance). You should have a some kind of
$modules member variable that lists all modules callable by __call. These
modules that get in this list is specified in a boolean argument in the
call to loadModule(), if TRUE the module functions will be callable using
overloading, if FALSE then it'll only be callable using
$mdb->extended->getOne() or such. The default value of this argument is up
to you.
[2005-02-14 17:09 UTC] smith at backendmedia dot com
yeah that sounds like a good idea. I was also thinking about allowing people to add a 2 letter prefix to the methods:
$this->exGetAll()
is converted to:
if(!is_object($this->extended)) {
$this->loadModule('extended');
}
$this->extended->getAll();
That might even be the cleanest solution. Every callable module just needs to register with that 2 letter prefix. That would also increase the performance.
You said in a previous mail that you would want this behaviour to be optional from the one you provided. I will ponder up the cleanest solution I can think of.
[2005-03-13 20:25 UTC] smith at backendmedia dot com
This bug has been fixed in CVS.
In case this was a documentation problem, the fix will show up at the
end of next Sunday (CET) on pear.php.net.
In case this was a pear.php.net website problem, the change will show
up on the website in short time.
Thank you for the report, and for helping us make PEAR better.