这个函数一看就非常有用,特别当你的类文件很多时,有网友测试,__autoload()对性能影响不大,当PHP引擎遇到未实例化的类是就会触发这个方法,比如说我写的博客系统,在初始化的时候我会这样做:
<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>
还有一种包含路径的方法:
<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>
即将下划线转换为目录分隔符(DIRECTORY_SEPARATOR),这样做会使你很有效的管理类库文件!
原文地址。。。
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });以上就介绍了PHP使用__autoload函数自动加载未定义的类,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。