Detailed examples of two PHP automatic loading implementation methods

伊谢尔伦
Release: 2023-03-12 08:30:02
Original
2769 people have browsed it

phpTwo implementation methods of automatic loading, friends in need can refer to it.

There are two ways to automatically load 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 to determine whether the file exists before include The problem.

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();
Copy after login

The second solution uses spl to automatically load. Let’s talk about this in detail.
spl_autoload_register()
A simple example

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();
Copy after login

spl_autoload_register() will automatically First call spl_autoload() 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_extenstions() to register the extension.
If not found In this case, you can also define the function to search
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 ');
There can be more...
How the MVC framework implements 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 obtaining the URL, parse out the controller and method.
Then set up automatic loading

The code is as follows:

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'));
Copy after login


Route, instantiate the controller, call the method, and what you write will start executing

The code is as follows:

/** 
* 路由 
*/ 
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'); 
}
Copy after login

The initial automatic loading is completed

The above is the detailed content of Detailed examples of two PHP automatic loading implementation methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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