亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Class library automatic loading

As the business becomes more and more complex, a script will need to include or require more and more class files,

At this time, you need to use the __autoload() method to automatically load the class file when instantiating the object

1, the use of __antoload()

Create a new init.php file with the following code:

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/3 0003
 * Time: 下午 3:39
 */
header('content-type:text/html;charset=utf8');
function __autoload($className){
    //自動加載類名為className,文件名為./$className.class.php的文件
    require "./$className.class.php";
}

2, test

Create new student class and teacher class

Student.class.php code is as follows:

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/3 0003
 * Time: 下午 3:41
 */
class Student{
    public function __construct()
    {
        echo "學生類已加載";
    }
}

Teacher.class.php code is as follows:

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/3 0003
 * Time: 下午 3:41
 */
class Teacher{
    public function __construct()
    {
        echo "老師類已加載";
    }
}

Create the index.php file to test whether the corresponding class file needs to be introduced at the same time to load the corresponding constructor

The code is as follows:

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/3 0003
 * Time: 下午 3:43
 */
require './init.php';
$student=new Student();
echo "<br>";
$teacher=new Teacher();

The effect of running the index.php file in the browser is as follows:

微信圖片_20180303155335.png

Continuing Learning
||
<?php echo "自動加載的使用";
submitReset Code