In external pages, there is no need to introduce class files, but the program will automatically "dynamically load" a class when it needs it.
Use the __autoload magic function
When two situations occur, this function will be called. This function requires us to define it in advance and write a general statement for loading class files in it
<code><span><span>function</span><span>__autoload</span><span>(<span>$name</span>)</span>{</span><span>require</span><span>'./lib/'</span>.<span>$name</span>.<span>'.class.php'</span>; }</code>
Use spl_autoload_register()
Use it to register (declare) multiple functions that can replace __autoload(). Naturally, you have to define these functions, and the functions of the functions are the same as __autoload(), but you can deal with more situations at this time
<code><span>//注册用于自动加载的函数</span> spl_autoload_register(<span>"model"</span>); spl_autoload_register(<span>"controll"</span>); <span>//分别定义两个函数</span><span><span>function</span><span>model</span><span>(<span>$name</span>)</span>{</span><span>$file</span> = <span>'./model/'</span>.<span>$name</span>.<span>'.class.php'</span>; <span>if</span>(file_exists(<span>$file</span>)){ <span>require</span><span>'./model/'</span>.<span>$name</span>.<span>'.class.php'</span>; } } <span>//如果需要一个类,但当前页面还没加载该类</span><span>//就会依次调用model()和controll(),直到找到该类文件加载,否则就报错</span><span><span>function</span><span>controll</span><span>(<span>$name</span>)</span>{</span><span>$file</span> = <span>'./controll/'</span>.<span>$name</span>.<span>'.class.php'</span>; <span>if</span>(file_exists(<span>$file</span>)){ <span>require</span><span>'./controll/'</span>.<span>$name</span>.<span>'.class.php'</span>; } }</code>
The above has introduced the automatic loading of PHP, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.