Home >Backend Development >PHP Tutorial >PHP uses __autoload function to automatically load undefined classes
This function is very useful at first glance, especially when you have a lot of class files. Some netizens have tested that __autoload() has little impact on performance. This method will be triggered when the PHP engine encounters an uninstantiated class. For example, when I write a blog system, I will do this during initialization:
<code><span>/** * 自动加载相关类库文件 * by www.phpddt.com */</span><span><span>function</span><span>__autoload</span><span>(<span>$classname</span>)</span>{</span><span>if</span>(substr(<span>$classname</span>,-<span>6</span>)==<span>"Action"</span>){ <span>include</span> APP_PATH.<span>'controllers/'</span>.<span>$classname</span>.<span>'.class.php'</span>; }<span>elseif</span>(substr(<span>$classname</span>, -<span>5</span>)==<span>"Model"</span>){ <span>include</span> APP_PATH.<span>'models/'</span>.<span>$classname</span>.<span>'.class.php'</span>; }<span>elseif</span>(<span>$classname</span>==<span>"Smarty"</span>){ <span>include</span> SYSTEM_PATH.<span>'smarty/Smarty.class.php'</span>; }<span>else</span>{ <span>include</span> APP_PATH.<span>'common/'</span>.<span>$classname</span>.<span>'.class.php'</span>; } }</code>
There is also a way to include the path:
<code><span><span>function</span><span>__autoload</span><span>(<span>$class_name</span>)</span> {</span><span>$path</span> = str_replace(<span>'_'</span>, DIRECTORY_SEPARATOR, <span>$class_name</span>); <span>require_once</span><span>$path</span>.<span>'.php'</span>; }</code>
That is, convert the underscore into a directory separator (DIRECTORY_SEPARATOR). This will make you very effective. Manage class library files!
Original address. . .
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });The above introduces how PHP uses the __autoload function to automatically load undefined classes, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.