PHP __autoload Automatic loading mechanism

Source: Internet
Author: User
Function: __autoload () future will be discarded (spl_autoload_register () substitution)
Function: When the class name error, the __autoload () use reason: When writing code will refer to a lot of require or include, this will cause code confusion and repeated references, and the automatic loading mechanism can solve the problem
I combine name SpaceAnd Auto LoadTo explain a simple example preface: Here I use the namespace corresponding to the specified directory menu, such as my rectangle namespace is controller, so that the directory structure will not error when loading automatically. 1 Create the directory structure as follows:

2) Code 01.php
<?php
/**
* Created by Phpstorm.
* User:lisam *
DATE:2016/11/14
* time:22:34
/use Controller\rectangle as Rectangle;  ① introduces the namespace

function __autoload ($class _name)  //② automatically loads the corresponding class name
{
$fileName = $class _name If the error occurs. ". class.php";
if (file_exists ($fileName)) {
    require_once ();
    Echo ' __autload class: ', $class _name, ' <br/> ';
}

$memo = new Rectangle ();
$memo->dumpobject ();



Rectanle.class.php
<?php
/**
* Created by Phpstorm.
* User:lisam *
DATE:2016/11/13
* time:22:53 * *
namespace Controller;

Class Rectangle
{
    private $width = 0;
    Private $height = 0;


    The Public Function Dumpobject ()
    {
        //Gets the class name, variable, method
        $class = Get_class ($this);//Find the current class name
        $attributes = Get _object_vars ($this);
        $methods = Get_class_methods ($this);

        Print information
        echo "



Modify:Replace __autoload () with the following:
Spl_autoload_register ("AutoLoad");  Spl_autoload_register inside uses a custom

function autoload ($class _name)
{
$fileName = $class _name. ". class.php";
if (file_exists ($fileName)) {
    require_once ($fileName);
    Echo ' __autload class: ', $class _name, ' <br/> ';
}



Other uses: spl_autoload_functions() Get loaded automatically: Get results as follows:



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.