Re: Why change require_once? A brief explanation of motives
| From: | Lukas Kahwe Smith | Date: | Tue, 17 Jul 2007 11:19:14 +0000 |
| Subject: | Re: Why change require_once? A brief explanation of motives | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-47548@lists.php.net to get a copy of this message | ||
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