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 * @version CVS: $Id: Autoloader.php 313023 2011-07-06 19:17:11Z dufuz $
14 * @link http://pear.php.net/manual/en/core.ppm.php#core.ppm.pear-autoloader
15 * @since File available since Release 0.1
16 * @deprecated File deprecated in Release 1.4.0a1
19 // /* vim: set expandtab tabstop=4 shiftwidth=4: */
21 if (!extension_loaded("overload")) {
22 // die hard without ext/overload
23 die("Rebuild PHP with the `overload' extension to use PEAR_Autoloader");
27 * Include for PEAR_Error and PEAR classes
29 require_once "PEAR.php";
32 * This class is for objects where you want to separate the code for
33 * some methods into separate classes. This is useful if you have a
34 * class with not-frequently-used methods that contain lots of code
35 * that you would like to avoid always parsing.
37 * The PEAR_Autoloader class provides autoloading and aggregation.
38 * The autoloading lets you set up in which classes the separated
39 * methods are found. Aggregation is the technique used to import new
40 * methods, an instance of each class providing separated methods is
41 * stored and called every time the aggregated method is called.
45 * @author Stig Bakken <ssb@php.net>
46 * @copyright 1997-2009 The Authors
47 * @license http://opensource.org/licenses/bsd-license.php New BSD License
48 * @version Release: 1.9.4
49 * @link http://pear.php.net/manual/en/core.ppm.php#core.ppm.pear-autoloader
50 * @since File available since Release 0.1
51 * @deprecated File deprecated in Release 1.4.0a1
53 class PEAR_Autoloader extends PEAR
58 * Map of methods and classes where they are defined
64 var $_autoload_map = array();
67 * Map of methods and aggregate objects
73 var $_method_map = array();
79 * Add one or more autoload entries.
81 * @param string $method which method to autoload
83 * @param string $classname (optional) which class to find the method in.
84 * If the $method parameter is an array, this
85 * parameter may be omitted (and will be ignored
86 * if not), and the $method parameter will be
87 * treated as an associative array with method
88 * names as keys and class names as values.
94 function addAutoload($method, $classname = null)
96 if (is_array($method)) {
97 array_walk($method, create_function('$a,&$b', '$b = strtolower($b);'));
98 $this->_autoload_map = array_merge($this->_autoload_map, $method);
100 $this->_autoload_map[strtolower($method)] = $classname;
105 // {{{ removeAutoload()
108 * Remove an autoload entry.
110 * @param string $method which method to remove the autoload entry for
112 * @return bool TRUE if an entry was removed, FALSE if not
116 function removeAutoload($method)
118 $method = strtolower($method);
119 $ok = isset($this->_autoload_map[$method]);
120 unset($this->_autoload_map[$method]);
125 // {{{ addAggregateObject()
128 * Add an aggregate object to this object. If the specified class
129 * is not defined, loading it will be attempted following PEAR's
130 * file naming scheme. All the methods in the class will be
131 * aggregated, except private ones (name starting with an
132 * underscore) and constructors.
134 * @param string $classname what class to instantiate for the object.
140 function addAggregateObject($classname)
142 $classname = strtolower($classname);
143 if (!class_exists($classname)) {
144 $include_file = preg_replace('/[^a-z0-9]/i', '_', $classname);
145 include_once $include_file;
147 $obj =& new $classname;
148 $methods = get_class_methods($classname);
149 foreach ($methods as $method) {
150 // don't import priviate methods and constructors
151 if ($method{0} != '_' && $method != $classname) {
152 $this->_method_map[$method] = $obj;
158 // {{{ removeAggregateObject()
161 * Remove an aggregate object.
163 * @param string $classname the class of the object to remove
165 * @return bool TRUE if an object was removed, FALSE if not
169 function removeAggregateObject($classname)
172 $classname = strtolower($classname);
173 reset($this->_method_map);
174 while (list($method, $obj) = each($this->_method_map)) {
175 if (is_a($obj, $classname)) {
176 unset($this->_method_map[$method]);
187 * Overloaded object call handler, called each time an
188 * undefined/aggregated method is invoked. This method repeats
189 * the call in the right aggregate object and passes on the return
192 * @param string $method which method that was called
194 * @param string $args An array of the parameters passed in the
197 * @return mixed The return value from the aggregated method, or a PEAR
198 * error if the called method was unknown.
200 function __call($method, $args, &$retval)
202 $method = strtolower($method);
203 if (empty($this->_method_map[$method]) && isset($this->_autoload_map[$method])) {
204 $this->addAggregateObject($this->_autoload_map[$method]);
206 if (isset($this->_method_map[$method])) {
207 $retval = call_user_func_array(array($this->_method_map[$method], $method), $args);
216 overload("PEAR_Autoloader");