Two implementation methods of php automatic loading_PHP tutorial

WBOY
Release: 2016-07-21 15:36:14
Original
849 people have browsed it

There are two ways to autoload PHP.
The first option is to use __autoload. This function is simpler and weaker.
But there is one problem that has not been solved, which is the problem of determining whether the file exists before include.

Copy code The code is as follows:

set_include_path('aa' . PATH_SEPARATOR . get_include_path());
function __autoload($ className)
{
//If you add this detection, because this file is not in the current directory, it will not detect the existence of the file.
//But include can succeed
if (file_exists ($className . '.php')) {
 include_once($className . '.php');
} else {
exit('no file');
}
}
$a = new Acls();

The second option uses spl to automatically load. Let’s talk about this in detail.
spl_autoload_register()
A simple example
Copy code The code is as follows:

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() will automatically call spl_autoload() first to find the ".php" program with a lowercase file name in the path. The default extension is ".ini", and you can also use spl_autoload_extensions() to register extensions name.
If it cannot be found, you can also search by defining your own function
such as
function loader1($class)
{
//Write some loading code yourself
}
function loader2($class)
{
//When loader1() cannot be found, I come to find it
}
spl_autoload_register('loader1');
spl_autoload_register('loader2');
You can do more...
How does the MVC framework implement automatic loading?
First set the path
'include' => array( 'application/catalog/controllers', 'application/catalog/models', ),$include = array('application/controllers', 'application/models', 'application/library');
set_include_path(get_include_path() . PATH_SEPARATOR .implode(PATH_SEPARATOR, $config['include']));
After getting the URL, parse out the controller and method.
Then set up automatic loading
Copy the code The code is as follows:

class Loader
{
/**
* Automatically load class
* @param $class class name
*/
public static function autoload($class)
{
$path = '';
$path = str_replace('_', '/', $class) . '.php';
include_once($path);
}
}
/**
* sql automatic loading
*/
spl_autoload_register(array('Loader', 'autoload'));

Route, instantiate controller, call method, What you write will start executing
Copy the code The code is as follows:

/**
* Routing
*/
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');
}

The initial automatic loading is completed

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322152.htmlTechArticleThere are two ways to autoload PHP. The first option is to use __autoload. This function is simpler and weaker. . But there is one problem that has not been solved, which is the problem of determining whether the file exists before include. Copy the code...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template