1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
<?xml version="1.0" encoding="utf-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.loader.spl-autoloader">
<title>The SplAutoloader Interface</title>
<sect2 id="zend.loader.spl-autoloader.intro">
<title>Overview</title>
<para>
While any valid PHP callback may be registered with
<function>spl_autoload_register()</function>, Zend Framework autoloaders often provide
more flexibility by being stateful and allowing configuration. To provide a common
interface, Zend Framework provides the <interfacename>SplAutoloader</interfacename>
interface.
</para>
<para>
Objects implementing this interface provide a standard mechanism for configuration, a
method that may be invoked to attempt to load a class, and a method for registering with
the SPL autoloading mechanism.
</para>
</sect2>
<sect2 id="zend.loader.spl-autoloader.quick-start">
<title>Quick Start</title>
<para>
To create your own autoloading mechanism, simply create a class implementing the
<interfacename>SplAutoloader</interfacename> interface (you may review the methods
defined in the <link linkend="zend.loader.spl-autoloader.methods">Methods
section</link>). As a simple example, consider the following autoloader, which will look
for a class file named after the class within a list of registered directories.
</para>
<programlisting language="php"><![CDATA[
require_once 'Zend/Loader/SplAutoloader.php';
class Custom_ModifiedIncludePathAutoloader implements Zend_Loader_SplAutoloader
{
protected $paths = array();
public function __construct($options = null)
{
if (null !== $options) {
$this->setOptions($options);
}
}
public function setOptions($options)
{
if (!is_array($options) && !($options instanceof Traversable)) {
throw new InvalidArgumentException();
}
foreach ($options as $path) {
if (!in_array($path, $this->paths)) {
$this->paths[] = $path;
}
}
return $this;
}
public function autoload($classname)
{
$filename = $classname . '.php';
foreach ($this->paths as $path) {
$test = $path . DIRECTORY_SEPARATOR . $filename;
if (file_exists($test)) {
return include($test);
}
}
return false;
}
public function register()
{
spl_autoload_register(array($this, 'autoload'));
}
}
]]></programlisting>
</sect2>
<sect2 id="zend.loader.spl-autoloader.options">
<title>Configuration Options</title>
<para>
This component defines no configuration options, as it is an interface.
</para>
</sect2>
<sect2 id="zend.loader.spl-autoloader.methods">
<title>Available Methods</title>
<variablelist>
<varlistentry id="zend.loader.spl-autoloader.methods.constructor">
<term>
<methodsynopsis>
<methodname>__construct</methodname>
<methodparam>
<funcparams>$options = null</funcparams>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Initialize and configure an autoloader
</para>
<para>
Autoloader constructors should optionally receive configuration options.
Typically, if received, these will be passed to the
<methodname>setOptions()</methodname> method to process.
</para>
</listitem>
</varlistentry>
<varlistentry id="zend.loader.spl-autoloader.methods.set-options">
<term>
<methodsynopsis>
<methodname>setOptions</methodname>
<methodparam>
<funcparams>$options</funcparams>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Configure the autoloader state
</para>
<para>
Used to configure the autoloader. Typically, it should expect either an array or
a <interfacename>Traversable</interfacename> object, though validation of the
options is left to implementation. Additionally, it is recommended that the
method return the autoloader instance in order to implement a fluent interface.
</para>
</listitem>
</varlistentry>
<varlistentry id="zend.loader.spl-autoloader.methods.autoload">
<term>
<methodsynopsis>
<methodname>autoload</methodname>
<methodparam>
<funcparams>$classname</funcparams>
</methodparam>
</methodsynopsis>
</term>
<listitem>
<para>
Attempt to resolve a class name to the file defining it
</para>
<para>
This method should be used to resolve a class name to the file defining it. When
a positive match is found, return the class name; otherwise, return a boolean
false.
</para>
</listitem>
</varlistentry>
<varlistentry id="zend.loader.spl-autoloader.methods.register">
<term>
<methodsynopsis>
<methodname>register</methodname>
</methodsynopsis>
</term>
<listitem>
<para>
Register the autoloader with the SPL autoloader
</para>
<para>
Should be used to register the autoloader instance with
<function>spl_autoload_register()</function>. Invariably, the method
should look like the following:
</para>
<programlisting language="php"><![CDATA[
public function register()
{
spl_autoload_register(array($this, 'autoload'));
}
]]></programlisting>
</listitem>
</varlistentry>
</variablelist>
</sect2>
<sect2 id="zend.loader.spl-autoloader.examples">
<title>Examples</title>
<para>
Please see the <link linkend="zend.loader.spl-autoloader.quick-start">Quick Start</link>
for a complete example.
</para>
</sect2>
</sect1>
|