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: