本文實例講述了PHP自定義函數(shù)實現(xiàn)assign()數(shù)組分配到模板及extract()變量分配到模板功能。分享給大家供大家參考,具體如下:
class base{
public $array;
public $key;
public $val;
public function assign($key,$val){
if(array($val)){
$this->array["$key"] = $val;
}else{
$this->array["$key"] = compact($val);
}
}
public function display($tpl){
$this->assign($this->key,$this->val);
extract($this->array);
if(file_exists($tpl)){ //模板存在就加載文件。
include $tpl;
}
}
}
class indexcontroller extends base{
public function index(){
$arr = array('a'=>'aaaaaaa','b'=>array('a'=>'111111','b'=>'22222','c'=>'3333'),'c'=>'ccccccc','d'=>'dddddd','e'=>'eeeee');
$str = '我是字符串';
$this->assign('arr',$arr);
$this->assign('str',$str);
$this->display('index.html');
}
}
$base = new base;
$base->index();
更多關于PHP相關內容感興趣的讀者可查看本站專題:《php面向對象程序設計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》