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

用spl_autoload_register()完成类的自动加载

坚果缘 2019-06-25 20:26:42 181
<?php
//先创建一个类在test文件夹中,该文件夹与当前文件同级
/* class animals //动物类 
{
 public $type;    //种类
 public $sex;     //性别
 public $habitat; //栖息地
 
 public function __construct($type='哺乳类',$sex='雄性',$habitat='山地') //参数带默认值
 {
	 $this->type=$type;
	 $this->sex=$sex;
	 $this->habitat=$habitat;	 
 }	
}  */

//通过注册自动加载器 自动加载类
spl_autoload_register(function ($className))
{
	require '../public/'.$className.'.php'; 
}

$panda = new animals();

$tunas = new animals('鱼类','雌性','海洋');

echo '这只熊猫是'.$panda->type.',性别为'.$panda->sex.',Ta 的栖息地是在'.$panda->habitat.'。<br>';
echo '这只金枪鱼是'.$tunas->type.',性别为'.$tunas->sex.',Ta 的栖息地是在'.$tunas->habitat.'。<br>';



?>


最新手记推荐

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

全部回复(0)我要回复

暂无评论~
  • 取消 回复 发送
  • PHP中文网