Re: Why change require_once? A brief explanation of motives
| From: | Pádraic Brady | Date: | Tue, 17 Jul 2007 11:57:12 +0000 |
| Subject: | Re: Why change require_once? A brief explanation of motives | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-47551@lists.php.net to get a copy of this message | ||
Still needs some tweaking; if I'm reading it right PEAR2::callAutoload() would just call
__autoload() if it exists, and skip SPL. It might be worth exploring a standard practice of using
the SPL and then falling back on spl_autoload_call(), or grabbing a list of such autoload functions
using spl_autoload_functions(). The second has the benefit of returning FALSE if nothing is
registered actually - could be a useful detection check for SPL usage by the user - and only then
attempt __autoload() directly.
Kind regards,
Paddy
Pádraic Brady
http://blog.astrumfutura.com
http://www..patternsforphp.com
----- Original Message ----
From: Lukas Kahwe Smith <mls@pooteeweet.org>
To: Pádraic Brady <padraic.brady@yahoo.com>
Cc: PEAR Dev <pear-dev@lists.php.net>
Sent: Tuesday, July 17, 2007 12:19:14 PM
Subject: Re: [PEAR-DEV] Why change require_once? A brief explanation of motives
Pádraic Brady wrote:
> Just to note we should not directly refer to "__autoload()" - many of us have
> applications already tying this function declaration up. We could offer an implementation (this was
> suggested earlier I think) capable of inclusion using the SPL functions which isn't going to
> conflict with existing __autoload() definitions.
foo.php would be user code.
Aside from that the fact that I am calling __autoload() directly is an
oversimplification. We obviously need to support both __autoload() as
well as the spl variant. I also just noticed that I forgot to wrap
things inside a class_exists() call in the factory.
Here is updated code, the spl detection and calling needs a bit more
tweaking probably, since I have not really used them so far.
===== PEAR2.php =====
class PEAR2
{
public static function callAutoload($class_name)
{
static $function = null;
if (is_null($function)) {
if (function_exists('__autoload')) {
$function = '__autoload';
} else if (spl_autoload_functions()) {
$function = 'spl_autoload_call';
} else {
throw new Exception('no autoload defined');
}
}
return $function($class_name);
}
}
===== Bar.php =====
class Bar
{
public static function factory($driver)
{
$class_name = 'Bar_'.$driver;
if (class_exists($class_name, false) ||
PEAR2::callAutoload($class_name)) {
return new $class_name;
}
throw new Exception('unable to load');
}
}
regards,
Lukas
____________________________________________________________________________________
Need Mail bonding?
Go to the Yahoo! Mail Q&A for great tips from Yahoo! Answers users.
http://answers.yahoo.com/dir/?link=list&sid=396546091