Re: Re: Silencing @
| From: | Greg Beaver | Date: | Mon, 31 Oct 2005 23:52:09 +0000 |
| Subject: | Re: Re: Silencing @ | ||
| References: | 1 2 3 | Groups: | php.pear.qa |
| Request: | Send a blank email to pear-qa+get-3206@lists.php.net to get a copy of this message | ||
Ian Eure wrote:
> What I typically do is:
>
> function factory($driver)
> {
> $file = 'My/Package/' . $driver . '.php;
> $class = 'My_Package_Driver';
> if (!file_exists($file)) {
> return PEAR::raiseError(...);
> }
> include_once $file;
> if (!class_exists($class)) {
> return PEAR::raiseError(...);
> }
> return new $class();
> }
>
> Any other suggestions? I don't really like using $driver without
> veriification. Perhaps filtering it through escapeshellarg()? Not sure
> this is all CS-worthy, but some best practices would be nice.
Better is:
function factory($driver)
{
$file = 'My/Package/' . $driver . '.php;
$class = 'My_Package_Driver';
if (class_exists($class)) {
return new $class;
}
$ip = get_include_path();
foreach (explode(PATH_SEPARATOR, $ip) as $fragment) {
if (file_exists($frag . DIRECTORY_SEPARATOR . $file)) {
include_once $file;
break;
}
}
if (!class_exists($class)) {
return PEAR::raiseError(...);
}
return new $class;
}
this scans the include_path. Your example will only work if the file is
in a subdirectory of the current directory.
Examples of a way to do this generically are in PEAR_Common::isIncludeable()
Greg