PHP automatic loading mechanism _php tutorial

Source: Internet
Author: User
Tags autoload spl
First, the method of implementing automatic loading in PHP

Use Require,include,require_once,include_once to load manually.

Use __autoload for automatic loading
Automatic loading using SPL's AutoLoad
Implementation of manual loading:

When there are few files to load, we can use the first one to complete. It's easy and it's no problem.


[PHP]
Require_once ' a.php ';
Require_once ' b.php ';
Require_once ' c.php ';

Require_once ' a.php ';
Require_once ' b.php ';
Require_once ' c.php ';
But is this OK when you need to load a lot of files? What do we do when we need to write 10, 20 require_once or more?

At this point we can use the __autoload method to simplify our code.

Implementation of __autoload Loading:

We create a in.php file in the test directory, which reads as follows.


[PHP]
Echo ' I am the in.php under test
';

Echo ' I am the in.php under test
'; then create a loader.php in the test directory, as follows.

[PHP]
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 here will output I'm the in.php under test

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 here will output I'm the in.php under test no problem, success! We can also create additional files to load, but what do you do when you need a lot of files to separate directories?

At this point we need to modify the loader.php to use the mapping to find the file to be loaded.


[PHP]
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 ();

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 map, so when the file structure changes, you do not need to modify the class name, just modify the corresponding item 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 someone else's project has a __autoload, so two __autoload conflict. The solution is to modify __ AutoLoad become a, this is undoubtedly very cumbersome, application scenario single.


The AutoLoad load implementation of SPL:

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


Related functions for automatic loading of SPL
Spl_autoload is the default implementation of _autoload (), which will go to Include_path to find $class_name (. php/.inc) Spl_autoload for automatic loading:
Create the in.php in the test directory as follows:


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

Class in {
Public Function index () {
Echo ' I am the in.php under test ';
}
}
?> in the test directory to establish the loader.php, the content is as follows
[HTML]
Set_include_path ("/var/www/test/"); Here you need to put the path into the include
Spl_autoload ("in"); Find/var/www/test/in.php
$in = new in ();
$in->index ();

Set_include_path ("/var/www/test/"); Here you need to put the path into the include
Spl_autoload ("in"); Find/var/www/test/in.php
$in = new in ();
$in->index ();
Spl_autoload_register registers the function with the SPL __autoload function stack and modifies the loader.php
[PHP]
function AutoLoad ($class) {
if ($class = = ' in ') {
Require_once ("/var/www/test/in.php");
}
}
Spl_autoload_register (' AutoLoad ');
$a = new in ();
$a->index ();

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

Spl_autoload_register registering multiple custom autoload function applications
First build the Mods folder in the test directory and set up the inmod.mod.php content as follows:
[PHP]
Class Inmod
{
function __construct ()
{
Echo ' I am in ' under mods;
}
}

Class Inmod
{
function __construct ()
{
Echo ' I am in ' under mods;
}
}
Then create the Libs folder in the test directory and build the inlib.lib.php content as follows:
[PHP]
Class Inlib
{
function __construct ()
{
Echo ' I am libs under ';
}
}

Class Inlib
{
function __construct ()
{
Echo ' I am libs under ';
}
Finally, build the loader.php content in the test directory 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 ();//OutputI'm under mods.
New Inlib ();//OutputI'm libs under.

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 I'm under mods.
New Inlib ();//Output I am libs under

http://www.bkjia.com/PHPjc/477470.html www.bkjia.com true http://www.bkjia.com/PHPjc/477470.html techarticle first, the method of automatic loading in PHP is loaded manually using Require,include,require_once,include_once. Use __autoload to automatically load autoload using SPL to implement ...

  • 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.