1、实现model使用效果
2、自动加载model,KJ.php
php
//自动加载文件
public static function _autoload($className){
switch ($className){
//自动model类
case substr($className,-5)=='Model':
$path= MODEL.'/'.$className.'.php';
if(is_file($path)) include $path;
break;
//自动加载控制器
case substr($className,-3)=='Crl':
$path= CONTROLLER.'/'.$className.'.php';
if(is_file($path)) include $path;
break;
//自动加载基类
case substr($className,-4)=='Base':
$path= KJ_CORE.'/base/'.$className.'.php';
if(is_file($path)) include $path;
break;
default :
break;
}
}
3、model可定义table,ModelBase.php
php
public function __construct($table=null){
if($table){
$this->table=$table;
}
if(!$this->table){
die("no table" );
}
$this->_connect();
$this->_opt();
}
4、创建model文件,testModel.php
php
<?php
class testModel extends ModelBase{
public $table='test';
public function findId($id)
{
return $this->where('id='.$id)->find();
}
}
5、调用model,indexCrl.php
php
<?php
class indexCrl{
public function index(){
$model=new testModel();
$data=$model->select();
var_dump($data);
$data2=$model->findId(1);
var_dump($data2);
}
}