Home
Backend Development
PHP Tutorial
Two implementation methods of php automatic loading_PHP tutorial



Two implementation methods of php automatic loading_PHP tutorial
Jul 21, 2016 pm 03:36 PM
autoload
php
load
accomplish
plan
method
have
use
of
automatic
this
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
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

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
3 weeks ago
By DDD

Hot tools Tags

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
3 weeks ago
By DDD

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
