10 * @author Stig Bakken <ssb@php.net>
11 * @copyright 1997-2009 The Authors
12 * @license http://opensource.org/licenses/bsd-license.php New BSD License
13 * @link http://pear.php.net/manual/en/core.ppm.php#core.ppm.pear-autoloader
14 * @since File available since Release 0.1
15 * @deprecated File deprecated in Release 1.4.0a1
18 // /* vim: set expandtab tabstop=4 shiftwidth=4: */
20 if (!extension_loaded("overload")) {
21 // die hard without ext/overload
22 die("Rebuild PHP with the `overload' extension to use PEAR_Autoloader");
26 * Include for PEAR_Error and PEAR classes
28 require_once "PEAR.php";
31 * This class is for objects where you want to separate the code for
32 * some methods into separate classes. This is useful if you have a
33 * class with not-frequently-used methods that contain lots of code
34 * that you would like to avoid always parsing.
36 * The PEAR_Autoloader class provides autoloading and aggregation.
37 * The autoloading lets you set up in which classes the separated
38 * methods are found. Aggregation is the technique used to import new
39 * methods, an instance of each class providing separated methods is
40 * stored and called every time the aggregated method is called.
44 * @author Stig Bakken <ssb@php.net>
45 * @copyright 1997-2009 The Authors
46 * @license http://opensource.org/licenses/bsd-license.php New BSD License
47 * @version Release: 1.10.1
48 * @link http://pear.php.net/manual/en/core.ppm.php#core.ppm.pear-autoloader
49 * @since File available since Release 0.1
50 * @deprecated File deprecated in Release 1.4.0a1
52 class PEAR_Autoloader extends PEAR
57 * Map of methods and classes where they are defined
63 var $_autoload_map = array();
66 * Map of methods and aggregate objects
72 var $_method_map = array();
78 * Add one or more autoload entries.
80 * @param string $method which method to autoload
82 * @param string $classname (optional) which class to find the method in.
83 * If the $method parameter is an array, this
84 * parameter may be omitted (and will be ignored
85 * if not), and the $method parameter will be
86 * treated as an associative array with method
87 * names as keys and class names as values.
93 function addAutoload($method, $classname = null)
95 if (is_array($method)) {
96 array_walk($method, create_function('$a,&$b', '$b = strtolower($b);'));
97 $this->_autoload_map = array_merge($this->_autoload_map, $method);
99 $this->_autoload_map[strtolower($method)] = $classname;
104 // {{{ removeAutoload()
107 * Remove an autoload entry.
109 * @param string $method which method to remove the autoload entry for
111 * @return bool TRUE if an entry was removed, FALSE if not
115 function removeAutoload($method)
117 $method = strtolower($method);
118 $ok = isset($this->_autoload_map[$method]);
119 unset($this->_autoload_map[$method]);
124 // {{{ addAggregateObject()
127 * Add an aggregate object to this object. If the specified class
128 * is not defined, loading it will be attempted following PEAR's
129 * file naming scheme. All the methods in the class will be
130 * aggregated, except private ones (name starting with an
131 * underscore) and constructors.
133 * @param string $classname what class to instantiate for the object.
139 function addAggregateObject($classname)
141 $classname = strtolower($classname);
142 if (!class_exists($classname)) {
143 $include_file = preg_replace('/[^a-z0-9]/i', '_', $classname);
144 include_once $include_file;
146 $obj = new $classname;
147 $methods = get_class_methods($classname);
148 foreach ($methods as $method) {
149 // don't import priviate methods and constructors
150 if ($method{0} != '_' && $method != $classname) {
151 $this->_method_map[$method] = $obj;
157 // {{{ removeAggregateObject()
160 * Remove an aggregate object.
162 * @param string $classname the class of the object to remove
164 * @return bool TRUE if an object was removed, FALSE if not
168 function removeAggregateObject($classname)
171 $classname = strtolower($classname);
172 reset($this->_method_map);
173 while (list($method, $obj) = each($this->_method_map)) {
174 if (is_a($obj, $classname)) {
175 unset($this->_method_map[$method]);
186 * Overloaded object call handler, called each time an
187 * undefined/aggregated method is invoked. This method repeats
188 * the call in the right aggregate object and passes on the return
191 * @param string $method which method that was called
193 * @param string $args An array of the parameters passed in the
196 * @return mixed The return value from the aggregated method, or a PEAR
197 * error if the called method was unknown.
199 function __call($method, $args, &$retval)
201 $method = strtolower($method);
202 if (empty($this->_method_map[$method]) && isset($this->_autoload_map[$method])) {
203 $this->addAggregateObject($this->_autoload_map[$method]);
205 if (isset($this->_method_map[$method])) {
206 $retval = call_user_func_array(array($this->_method_map[$method], $method), $args);
215 overload("PEAR_Autoloader");