<!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. You may define an <span class="function"><a href="function.autoload.html" class="function">__autoload()</a></span> function which is automatically called in case you are trying to use a class/interface which hasn't been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error. </p> <div class="tip"><strong class="tip">Tip</strong> <p class="para"> <span class="function"><a href="function.spl-autoload-register.html" class="function">spl_autoload_register()</a></span> provides a more flexible alternative for autoloading classes. For this reason, using <span class="function"><a href="function.autoload.html" class="function">__autoload()</a></span> is discouraged and may be deprecated or removed in the future. </p> </div> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Prior to 5.3.0, exceptions thrown in the __autoload 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 5.3.0+ exceptions thrown in the __autoload function can be caught in the <a href="language.exceptions.html" class="link"> catch</a> block, with 1 provision. If throwing a custom exception, then the custom exception class must be available. The __autoload 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-180"> <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 /></span><span style="color: #007700">function </span><span style="color: #0000BB">__autoload</span><span style="color: #007700">(</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-181"> <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 /></span><span style="color: #007700">function </span><span style="color: #0000BB">__autoload</span><span style="color: #007700">(</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-182"> <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 /></span><span style="color: #007700">function </span><span style="color: #0000BB">__autoload</span><span style="color: #007700">(</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-183"> <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 /></span><span style="color: #007700">function </span><span style="color: #0000BB">__autoload</span><span style="color: #007700">(</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#unserialize-callback-func" class="link">unserialize_callback_func</a></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.spl-autoload-register.html" class="function">spl_autoload_register()</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>