完成类的自动加载案例

Original 2019-03-29 18:07:26 201
abstract:Car.php: <?php class Car {     public $name;     public $brand;     public $price;     public&
Car.php:
<?php


class Car
{
    public $name;
    public $brand;
    public $price;

    public function __construct($name,$brand,$price)
    {
          $this->name = $name;
          $this->brand = $brand;
          $this->price = $price;
    }
}

Phone.php:
<?php

class Phone
{
    public $name;
    public $brand;
    public $price;

    public function __construct($name,$brand,$price)
    {
        $this->name = $name;
        $this->brand = $brand;
        $this->price = $price;
    }
}

demo.php:
<?php
//类的自动加载


//自动加载器:最重要的一个参数就是一个回调
spl_autoload_register(function($className){
//    include 'public/'.$className.'.php';

    //推荐使用绝对路径
    require __DIR__.'/public/'.$className.'.php';
});

 $car = new Car('丰田','汉兰达',350000);
 $phone = new Phone('华为','P20',7800);

 echo $car->name.$car->brand.' :'.$car->price,'<br>';
 echo $phone->name.$phone->brand.' :'.$phone->price,'<br>';


Correcting teacher:天蓬老师Correction time:2019-03-30 10:32:08
Teacher's summary:类的自动加载, 是现在框架的基本功能之一, 这里是底层的实现原理, 好好理解一下

Release Notes

Popular Entries