返回完成类的自动加......登陆

完成类的自动加载案例

Jia2019-03-29 18:07:26201
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>';


最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送