<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Autoloading Classes</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.constants.html">Class Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop5.decon.html">Constructors and Destructors</a></div> <div class="up"><a href="language.oop5.html">Classes and Objects</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="language.oop5.autoload" class="sect1"> <h2 class="title">Autoloading Classes</h2> <p class="para"> Many developers writing object-oriented applications create one PHP source file per class definition. One of the biggest annoyances is having to write a long list of needed includes at the beginning of each script (one for each class). </p> <p class="para"> In PHP 5, this is no longer necessary. The <span class="function"><a href="function.spl-autoload-register.html" class="function">spl_autoload_register()</a></span> function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error. </p> <div class="tip"><strong class="tip">Tip</strong> <p class="para"> Although the <span class="function"><a href="function.autoload.html" class="function">__autoload()</a></span> function can also be used for autoloading classes and interfaces, it's preferred to use the <span class="function"><a href="function.spl-autoload-register.html" class="function">spl_autoload_register()</a></span> function. This is because it is a more flexible alternative (enabling for any number of autoloaders to be specified in the application, such as in third party libraries). For this reason, using <span class="function"><a href="function.autoload.html" class="function">__autoload()</a></span> is discouraged and it may be deprecated in the future. </p> </div> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Prior to PHP 5.3, exceptions thrown in the <span class="function"><a href="function.autoload.html" class="function">__autoload()</a></span> function could not be caught in the <a href="language.exceptions.html" class="link">catch</a> block and would result in a fatal error. From PHP 5.3 and upwards, this is possible provided that if a custom exception is thrown, then the custom exception class is available. The <span class="function"><a href="function.autoload.html" class="function">__autoload()</a></span> function may be used recursively to autoload the custom exception class. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Autoloading is not available if using PHP in CLI <a href="features.commandline.html" class="link">interactive mode</a>. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> If the class name is used e.g. in <span class="function"><a href="function.call-user-func.html" class="function">call_user_func()</a></span> then it can contain some dangerous characters such as <em>../</em>. It is recommended to not use the user-input in such functions or at least verify the input in <span class="function"><a href="function.autoload.html" class="function">__autoload()</a></span>. </p> </p></blockquote> <p class="para"> <div class="example" id="example-187"> <p><strong>Example #1 Autoload example</strong></p> <div class="example-contents"><p> This example attempts to load the classes <em>MyClass1</em> and <em>MyClass2</em> from the files <var class="filename">MyClass1.php</var> and <var class="filename">MyClass2.php</var> respectively. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />spl_autoload_register</span><span style="color: #007700">(function (</span><span style="color: #0000BB">$class_name</span><span style="color: #007700">) {<br /> include </span><span style="color: #0000BB">$class_name </span><span style="color: #007700">. </span><span style="color: #DD0000">'.php'</span><span style="color: #007700">;<br />});<br /><br /></span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">MyClass1</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$obj2 </span><span style="color: #007700">= new </span><span style="color: #0000BB">MyClass2</span><span style="color: #007700">(); <br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-188"> <p><strong>Example #2 Autoload other example</strong></p> <div class="example-contents"><p> This example attempts to load the interface <em>ITest</em>. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />spl_autoload_register</span><span style="color: #007700">(function (</span><span style="color: #0000BB">$name</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">);<br />});<br /><br />class </span><span style="color: #0000BB">Foo </span><span style="color: #007700">implements </span><span style="color: #0000BB">ITest </span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #FF8000">/*<br />string(5) "ITest"<br /><br />Fatal error: Interface 'ITest' not found in ...<br />*/<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-189"> <p><strong>Example #3 Autoloading with exception handling for 5.3.0+</strong></p> <div class="example-contents"><p> This example throws an exception and demonstrates the try/catch block. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />spl_autoload_register</span><span style="color: #007700">(function (</span><span style="color: #0000BB">$name</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Want to load </span><span style="color: #0000BB">$name</span><span style="color: #DD0000">.\n"</span><span style="color: #007700">;<br /> throw new </span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">"Unable to load </span><span style="color: #0000BB">$name</span><span style="color: #DD0000">."</span><span style="color: #007700">);<br />});<br /><br />try {<br /> </span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">NonLoadableClass</span><span style="color: #007700">();<br />} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">$e</span><span style="color: #007700">-></span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Want to load NonLoadableClass. Unable to load NonLoadableClass. </pre></div> </div> </div> <div class="example" id="example-190"> <p><strong>Example #4 Autoloading with exception handling for 5.3.0+ - Missing custom exception</strong></p> <div class="example-contents"><p> This example throws an exception for a non-loadable, custom exception. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />spl_autoload_register</span><span style="color: #007700">(function (</span><span style="color: #0000BB">$name</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Want to load </span><span style="color: #0000BB">$name</span><span style="color: #DD0000">.\n"</span><span style="color: #007700">;<br /> throw new </span><span style="color: #0000BB">MissingException</span><span style="color: #007700">(</span><span style="color: #DD0000">"Unable to load </span><span style="color: #0000BB">$name</span><span style="color: #DD0000">."</span><span style="color: #007700">);<br />});<br /><br />try {<br /> </span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">NonLoadableClass</span><span style="color: #007700">();<br />} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">$e</span><span style="color: #007700">-></span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Want to load NonLoadableClass. Want to load MissingException. Fatal error: Class 'MissingException' not found in testMissingException.php on line 4 </pre></div> </div> </div> </p> <div class="simplesect"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="function.unserialize.html" class="function">unserialize()</a></span></li> <li class="member"><a href="var.configuration.html#ini.unserialize-callback-func" class="link">unserialize_callback_func</a></li> <li class="member"><span class="function"><a href="function.spl-autoload-register.html" class="function">spl_autoload_register()</a></span></li> <li class="member"><span class="function"><a href="function.spl-autoload.html" class="function">spl_autoload()</a></span></li> <li class="member"><span class="function"><a href="function.autoload.html" class="function">__autoload()</a></span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.constants.html">Class Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop5.decon.html">Constructors and Destructors</a></div> <div class="up"><a href="language.oop5.html">Classes and Objects</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>