Automatic loading of PHP classes

不言
Release: 2023-03-24 20:40:01
Original
1567 people have browsed it

The content of this article is about the automatic loading of PHP classes. It has a certain reference value. Now I share it with everyone. Friends in need can refer to it.

Write a HumanModel.php## first. #

class HumanModel {
    public function t() {
        echo &#39;人类<br >&#39;;
    }
}
Copy after login
Copy after login

// =Example= //

require(&#39;./HumanModel.php&#39;);$lisi = new HumanModel();$lisi->t(); // 人类
Copy after login
Copy after login

// =Automatic loading of classes= //

function __autoload($c) {
    echo &#39;~~~~~~~~~&#39;,$c,&#39;~~~~~~~~&#39;;
}$ming = new Dog();
Copy after login
Copy after login

// === Notes Part 1===

/*

Automatic loading of classes

If a non-existent class is called,

Before reporting an error, we can also use the __autoload function
Get an intervention opportunity

The system will call the __autoload() function

and automatically pass the "class name" to the __autoload function

We can load in __autoload Required classes

*/

function __autoload($c) {
    echo &#39;我先自动加载&#39;;    echo &#39;./&#39; . $c . &#39;.php&#39;;    echo &#39;<br >&#39;;    require(&#39;./&#39; . $c . &#39;.php&#39;);
}$lisi = new HumanModel();$lisi->t();// 我先自动加载./HumanModel.php// 人类function test() {
    // 函数内可以写任何合法的PHP代码,包含再声明一个函数/类
    class Bird {
        public static function sing() {
            echo &#39;百灵鸟放声歌唱!<br >&#39;;
        }
    }
}// 必须要调用函数然后才能执行内部test();
Bird::sing(); 


/*
Bird::sing(); 

未定义类
转到上面类的自动加载函数中,于是报错如下:
Warning: require(./Bird.php): failed to open stream: No such file or directory
找不到Bird这个php
*/
Copy after login
Copy after login

// ===Notes Part 2===

/*

Can automatic loading only use the __autoload function?
Answer: No, in fact, you can also specify a function.

For example, use the zidongjiazai() function

Note: We need to notify the system that we have written an automatic loading method

Use the system function spl_autoload_register to notify
*/

spl_autoload_register(&#39;zidongjiazai&#39;);function zidongjiazai($c) {
    require(&#39;./&#39; . $c . &#39;.php&#39;);
}$HumanModel = new HumanModel();$HumanModel->t();
Copy after login
Copy after login

// Tips: We can also register a static method of the class to use as the automatic loading function

First write a HumanModel.php

class HumanModel {
    public function t() {
        echo &#39;人类<br >&#39;;
    }
}
Copy after login
Copy after login

// =Example= //

require(&#39;./HumanModel.php&#39;);$lisi = new HumanModel();$lisi->t(); // 人类
Copy after login
Copy after login

// =Automatic loading of classes= //

function __autoload($c) {
    echo &#39;~~~~~~~~~&#39;,$c,&#39;~~~~~~~~&#39;;
}$ming = new Dog();
Copy after login
Copy after login

// ===Notes Part 1===

/*

Automatic loading of classes

If a non-existent class is called,

Before reporting an error, we can also use the __autoload function
Get an intervention opportunity

The system will call the __autoload() function

and automatically pass the "class name" to the __autoload function

We can load the needs in __autoload Class

*/

function __autoload($c) {
    echo &#39;我先自动加载&#39;;    echo &#39;./&#39; . $c . &#39;.php&#39;;    echo &#39;<br >&#39;;    require(&#39;./&#39; . $c . &#39;.php&#39;);
}$lisi = new HumanModel();$lisi->t();// 我先自动加载./HumanModel.php// 人类function test() {
    // 函数内可以写任何合法的PHP代码,包含再声明一个函数/类
    class Bird {
        public static function sing() {
            echo &#39;百灵鸟放声歌唱!<br >&#39;;
        }
    }
}// 必须要调用函数然后才能执行内部test();
Bird::sing(); 


/*
Bird::sing(); 

未定义类
转到上面类的自动加载函数中,于是报错如下:
Warning: require(./Bird.php): failed to open stream: No such file or directory
找不到Bird这个php
*/
Copy after login
Copy after login

// ===Notes Part 2===

/*

Can automatic loading only use the __autoload function?
Answer: No, in fact, you can also specify a function.

For example, use the zidongjiazai() function

Note: We need to notify the system that we have written an automatic loading method

Use the system function spl_autoload_register to notify
*/

spl_autoload_register(&#39;zidongjiazai&#39;);function zidongjiazai($c) {
    require(&#39;./&#39; . $c . &#39;.php&#39;);
}$HumanModel = new HumanModel();$HumanModel->t();
Copy after login
Copy after login

// Tips: We can also register a static method of the class to act as an automatic loading function

Related recommendations:

PHP’s abstract class

The above is the detailed content of Automatic loading of PHP classes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!