php__autoload()方法是什么,怎么用的?

PHP / 633人浏览 / 0人评论

php __autoload(),php魔术函数__autoload()方法出现以前,假如你要在一个程序文件中实例化多个对象,那么你必须用include或者require包含进来这些类文件,或者你把这些类定义在同一个类文件中——相信这个文件一定会非常大。但是__autoload()方法出来,这个类会在你实例化对象之前自动加载制定的类文件。

PHP5中的特殊方法

在php中内置了一些特书的方法,这些特殊方法被称之为魔术方法,其也有特殊的命名规则,以“__”双下划线开头的方法和函数名,所以我们自定义的函数不应该用双下划下开头,目的是为了区分魔术方法。

PHP魔术方法有哪些呢?

__construct()__autoload()__destruct()__call()__callStatic()__get()
__set()__isset()__unset()__sleep()__wakeup()__toString()__invoke()__set_state()__clone()__debugInfo() 等方法在 PHP 中被称为魔术方法(Magic methods)。在命名自己的类方法时不能使用这些方法名,除非是想使用其魔术功能。

php__autoload()概述

php __autoload(),php魔术函数__autoload()方法出现以前,假如你要在一个项目程序文件中实例化多个对象,那么你必须用include或者require包含进来这些类文件,或者你把这些类定义在同一个类文件中,相信这个文件一定会非常大。为此PHP提供了 __autoload()方法,它会在试图使用尚未被定义的类时自动调用。通过调用此函数,脚本引擎在 PHP 出错失败前有了最后一个机会加载所需的类。

支持版本:

(PHP 5, PHP 7) PHP 7.2.0 起废弃

php__autoload()演示示例

在这个演示示例中,我们定义三个类Car、Clothes、Person,Car和Clothes继承了Person类,在定义个index.php文件,我们在index.php文件中使用__autoload()方法,并且在__autoload()方法下方直接实例化想要实例化的类,当所实例化的类不存在的时候,则会自动调用__autoload()方法。
index.php文件代码

<?php
function __autoload($classname){
    $classpath="./".$classname.'.php';
    if(file_exists($classpath)){
        require_once($classpath);
    }
    else{        
        echo 'class file'.$classpath.'not found!';
    }
}
$clothes_obj = new Clothes('红色', 500);
$clothes_obj->get_clothes_infor();
$car_obj = new Car('蓝色', 6);
$car_obj->get_car_infor();
/*
//输出一些结果
This is Person class!
名字:王浩
年龄:25岁
This is Clothes class!
衣服的颜色:红色
衣服的价格:500元
This is Person class!
名字:王浩
年龄:25岁
This is Car class!
车的颜色是:蓝色
车的座位数是:6个
*/

Car类 Car.php文件代码

<?php
//定义一个类Car,文件名为Car.php,Car继承Person
class Car extends Person
{
    private $color;
    
    private $site_number;
    
    public function __construct($color, $site_number){
        parent::__construct('王浩', 25);
        $this->get_person_infor();
        $this->color = $color;
        $this->site_number  = $site_number;        
        echo "<br>";
        echo "This is Car class!";
    }
    
    function get_car_infor() {
        
        echo "<br />";
        echo "车的颜色是:".$this->color."<br />";
        echo " 车的座位数是:".$this->site_number.'个';
    }
    
}

Clothes类 Clothes.php文件代码

<?php
//定义一个类Clothes,文件名为Clothes.php,Clothes继承Person
class Clothes extends Person
{
    private $color;
    
    private $price;
    
    public function __construct($color, $price){
        parent::__construct('王浩', 25);
        $this->get_person_infor();
        $this->color = $color;
        $this->price = $price;
        echo "<br>";
        echo "This is Clothes class!";
    }
    
    function get_clothes_infor() {
        echo "<br />";
        echo "衣服的颜色:".$this->color."<br />";
        echo " 衣服的价格:".$this->price.'元<br />';
    }
    
}

Person类 Person.php文件代码

<?php
//定义Person类
class Person
{
    private $name;
    
    private $age;
    
    public function __construct($name, $age){
        $this->name = $name;
        $this->age  = $age;
        echo "This is Person class!";
    }
    
    function get_person_infor() {
        echo "<br />";
        echo "名字:".$this->name."<br />";
        echo " 年龄:".$this->age.'岁';
    }
    
}

转载注明:

0 条评论

还没有人发表评论

发表评论 取消回复

记住我的信息,方便下次评论
有人回复时邮件通知我