php自动加载
php自动载方法有两种.
第一种方案用__autoload,这个函数较简单,也较弱.
但有一问题没有解决, 就是在include前判断文件是否存在的问题.
set_include_path('aa' . PATH_SEPARATOR . get_include_path()); function __autoload($className) { //如果加这个检测, 因为此文件不在当前目录下,它就会检测不到文件存在, //但include是能成功的 if (file_exists($className . '.php')) { include_once($className . '.php'); } else { exit('no file'); } } $a = new Acls();
第二种方案用spl自动加载,这里具体说一下这个.
spl_autoload_register()
一个简单的例子
set_include_path('aa' . PATH_SEPARATOR . get_include_path()); //function __autoload($className) //{ // if (file_exists($className . '.php')) { // include_once($className . '.php'); // } else { // exit('no file'); // } //} spl_autoload_register(); $a = new Acls();
spl_autoload_register()会自动先调用spl_autoload()在路径中查找具有小写文件名的".php"程序.默认查找的扩展名还有".ini",还可以用spl_autoload_extenstions()注册扩展名.
在找不到的清况下,还可以通过自己定义函数查找
如
function loader1($class)
{
//自己写一些加载的代码
}
function loader2($class)
{
//当loader1()找不到时,我来找
}
spl_autoload_register('loader1');
spl_autoload_register('loader2');
还可以更多........
MVC框架是如何实现自动加载的
首先设置路径
$include = array('application/controllers', 'application/models', 'application/library');
set_include_path(get_include_path() . PATH_SEPARATOR .implode(PATH_SEPARATOR, $config['include']));
在获取URL,解析出控制器与方法.
然后设置自动加载
class Loader { /** * 自动加载类 * @param $class 类名 */ public static function autoload($class) { $path = ''; $path = str_replace('_', '/', $class) . '.php'; include_once($path); } } /** * sql自动加载 */ spl_autoload_register(array('Loader', 'autoload'));
/** * 路由 */ public function route() { if (class_exists($this->getController())) { $rc = new ReflectionClass($this->getController()); if ($rc->hasMethod($this->getAction())) { $controller = $rc->newInstance(); $method = $rc->getMethod($this->getAction()); $method->invoke($controller); } else throw new Exception('no action'); } else throw new Exception('no controller'); }
初步的自动加载就完成了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
