Re: [Fwd: Re: PEAR framework changes for PHP5]
| From: | Jon Parise | Date: | Sat, 31 Jan 2004 01:06:37 +0000 |
| Subject: | Re: [Fwd: Re: PEAR framework changes for PHP5] | ||
| References: | 1 2 3 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-25358@lists.php.net to get a copy of this message | ||
On Fri, Jan 30, 2004 at 11:28:54AM -0500, Hans Lellelid wrote:
> On a related note, I believe PEAR tools would get far more use if they
> could integrate more easily into other applications. One thing that I
> think would facilitate this greatly is having a class path convention.
> I should be able to write (e.g.) my own Log container or MDB driver and
> specify a full path to that class when using the Log::factory(),
> MDB::factory() method, etc. My web application does not live in
> /usr/local/lib/php, and I don't want my container files to have to be
> copied there so they can be found by the factory method. One solution
> would be passing the actual path to the file (assumes 1-class per file);
> a better (IMO) solution might be to have an [optional] array convention
> -- e.g. array('filepath', 'classname'). I like the array idea because
> it's kinda like what PHP does for specifying a method to be invoked
> (e.g. by call_user_func). I've used dot-notation
> (classpath.relative.to.include.ClassName) for many of my tools because
> it provides at least a layer of abstraction between filesystem and
> classname, but that isn't ideal either and wouldn't work for PEAR where
> filenames and classnames do not match.
It's probably non-obvious, but the Log package already supports this.
Here's the relevant code from the Log::factory() implementation:
$handler = strtolower($handler);
$class = 'Log_' . $handler;
$classfile = 'Log/' . $handler . '.php';
/*
* Attempt to include our version of the named class, but don't treat
* a failure as fatal. The caller may have already included their own
* version of the named class.
*/
@include_once $classfile;
/* If the class exists, return a new instance of it. */
if (class_exists($class)) {
return new $class($name, $ident, $conf, $level);
}
This allows you to use your own, local handler implementation by
including it before the call to Log::factory():
require_once 'Log.php';
require_once 'local/stuff/Log_custom.php';
$log =& Log:factory('custom');
--
Jon Parise (jon@php.net) :: The PHP Project (http://www.php.net/)