返回独立完成类的自......登陆

独立完成类的自动加载案例,从此告别include/require

红色熊猫2019-05-21 11:33:50181

shop.php


<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/5/21
* Time: 11:11
*/

class  Shop
{
   public  $name;
   public  $price;
   public  $catid;
   public  $lei='shop';

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

   function  getDetail(){
       return '商品名称'.$this->name."<br>".'商品价格'.':'.$this->price.'<br>'.'商品分类'.":".$this->catid;
   }
}





car.php


<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/5/21
* Time: 11:11
*/

class  Car
{
   public  $name;
   public  $price;
   public  $catid;
   public  $lei='car';


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

   function  getDetail(){
       return '商品名称'.$this->name."<br>".'商品价格'.':'.$this->price.'<br>'.'商品分类'.":".$this->catid;
   }
}



new.php 

<?php
header("Content-type: text/html; charset=utf-8");
date_default_timezone_set("PRC");

spl_autoload_register(function ($className){
   include  __DIR__.'/commonClass/'.$className.'.php';
});

$shop = new Shop('上衣',23,'服装');
echo  $shop->getDetail(),'<br>';
echo  $shop->lei;echo "<br>";



$car = new Car('大众','30万','奔腾');
echo  $car->getDetail();echo "<br>";
echo $car->lei;




?>

最新手记推荐

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

全部回复(0)我要回复

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