返回用spl_au......登陆

用spl_autoload_register实现类的自动的加载

奔跑2019-03-26 13:16:24193

// public目录和spl_autoload.php文件在同一个目录,pupblic目录下有三个文件,分别是Base.php,Computer.php,Mobile.php

//spl_autoload.php文件内容:

<?php


spl_autoload_register(function ($className) {

   require __DIR__."/public/{$className}.php"; //推荐绝对路径。

});

$com = new Computer('联想', '扬天', 5000);

$mobile = new Mobile('华为', 'P20', 7800);

$com->show();

$mobile->show();

//Base.php文件内容:

<?php


class Base

{

public function show()

{

echo $this->brand.$this->model.'的价格:'.$this->price, '<hr>';

}

}

//Computer.php文件内容:

<?php


class Computer extends Base

{

public $brand;

public $model;

public $price;


public function __construct($brand, $model, $price)

{

$this->brand = $brand;

$this->model = $model;

$this->price = $price;

}

}


//Mobile文件内容:

<?php


class Mobile extends Base

{

public $brand;

public $model;

public $price;


public function __construct($brand, $model, $price)

{

$this->brand = $brand;

$this->model = $model;

$this->price = $price;

}

}


最新手记推荐

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

全部回复(0)我要回复

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