An in-depth analysis of PHP automatic loading mechanism _php skills

Source: Internet
Author: User
Tags autoload spl

How to implement automatic loading in PHP
1. Use require,include,require_once,include_once to load manually.
2. Use __autoload for automatic loading
3. Use SPL's autoload to implement automatic loading
Manual-Loaded implementations:

We can use the first one to complete when there are few files to load. It's simple and no problem.

Copy Code code as follows:

Require_once ' a.php ';
Require_once ' b.php ';
Require_once ' c.php ';

But is it okay to do this when you need to load a lot of files? What do we do when we need to write 10, 20 require_once or more?

This time we can use the __autoload method to simplify our code.

Implementation of __autoload Loading:
We created a in.php file under the test directory, which reads as follows.

Copy Code code as follows:

Echo ' I am the in.php<br/> ' under test;

Then create a loader.php under the test directory, which reads as follows.
Copy Code code as follows:

You need to overload the __autoload method to customize the path that contains the class file
function __autoload ($classname)
{
$class _file = Strtolower ($classname). ". PHP ";
if (file_exists ($class _file)) {
Require_once ($class _file);
}
}
@ $test = new in (); Execution to this will output <span style= "font-family:arial, Helvetica, Sans-serif" > I was under test under the in.php</span>

No problem, it's done! We can also create other files to load, but what happens when there are a lot of files that need to be divided into directories?

Then we need to modify the loader.php can use the mapping to find the file to be loaded.

Copy Code code as follows:

function __autoload ($class _name) {
$map = Array (
' Index ' => './include/index.php ',
' In ' => './in.php '
);

if (file_exists ($map [$class _name]) && isset ($map [$class _name])) {
Require_once $map [$class _name];
}
}
New index ();


The advantage of this approach is that the class name and file path are only maintained with a single mapping, so when the file structure changes, you do not need to modify the class name, you just need to modify the corresponding items in the map.

But __autoload can only be used once in a project, when your project refers to someone else's project, there is a __autoload in your project, and there is a __autoload for someone else's project, so the two __autoload conflict. The solution is to modify __ AutoLoad become one, which is undoubtedly very tedious, single application scenario.

SPL's autoload Load implementation:
SPL's AutoLoad series functions use a autoload call stack, and you can use Spl_autoload_register to register multiple custom autoload functions, with a wide range of application scenarios

• Establish in.php under the test catalogue, as follows

Copy Code code as follows:

<?php
Class in {
Public Function index () {
Echo ' I am the in.php ' under test;
}
}
?>

The loader.php is established under the test directory, and the contents are as follows
Copy Code code as follows:

<?php
Set_include_path ("/var/www/test/"); Here you need to put the path into include
Spl_autoload ("in"); Looking for/var/www/test/in.php
$in = new in ();
$in->index ();

Spl_autoload_register registers the function into the SPL __autoload function stack, modifying the loader.php
Copy Code code as follows:

function AutoLoad ($class) {
if ($class = = ' in ') {
Require_once ("/var/www/test/in.php");
}
}
Spl_autoload_register (' AutoLoad ');
$a = new in ();
$a->index ();

Application of Spl_autoload_register to register multiple custom autoload functions
First, create the Mods folder under the test directory and establish the inmod.mod.php contents as follows:
Copy Code code as follows:

<?php
Class Inmod
{
function __construct ()
{
Echo ' I am under the mods ';
}
}

The Libs folder is then created under the test directory and the inlib.lib.php content is set up as follows:
Copy Code code as follows:

<?php
Class Inlib
{
function __construct ()
{
Echo ' I am under the Libs ';
}
}

Finally, the content of loader.php is established under the test catalogue as follows
Copy Code code as follows:

<?php
Class Loader {
/**
* Auto Load Class
* @param $class class name
*/
public static function mods ($class) {
if ($class) {
Set_include_path ("/var/www/test/mods/");
Spl_autoload_extensions (". mod.php");
Spl_autoload (Strtolower ($class));
}
}
public static function libs ($class) {
if ($class) {
Set_include_path ("/var/www/test/libs/");
Spl_autoload_extensions (". lib.php");
Spl_autoload (Strtolower ($class));
}
}
}
Spl_autoload_register (Array (' Loader ', ' mods '));
Spl_autoload_register (Array (' Loader ', ' Libs '));
New Inmod ()//Output <span style= "font-family: ' Times New Roman ';" Font-size:14px "> I am the in</span> under the mods
New Inlib ();//<span style= "font-family:arial, Helvetica, Sans-serif" > Output </span><span style= " Font-family: ' Times New Roman '; Font-size:14px "> I am a in</span> under Libs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.