Ec (2); php class usage example tutorial & lt ;? Php ** & nbsp; * Classprogramforyinghua05-2 & nbsp; * designer: songsong & nbsp; * classTemplate {& nbsp; var $ tpl_vars; & nbsp; var $ tpl_path; & nbsp; var $ _ debug; & nbsp script ec (2); script
Tutorial on using php classes
/**
* Class program for yinghua05-2
* Designer: songsong
*/
Class Template {
Var $ tpl_vars;
Var $ tpl_path;
Var $ _ debug;
/**
* Construct for Template
* PHP5 or upper version
*/
Function _ construct (){
$ This-> Template ();
}
/**
* Construct for Template
*
* @ Return Template
*/
Function Template (){
$ This-> tpl_vars = array ();
$ This-> tpl_path = '';
$ This-> _ debug = false;
}
/**
* Set template path
*
* @ Param string $ path
* @ Return boolean
*/
Function setPath ($ path ){
If (is_dir ($ path )){
$ Path = rtrim ($ path ,'/').'/';
$ This-> tpl_path = $ path;
Return true;
} Else {
If ($ this-> _ debug ){
$ This-> _ debug ('template path is not exists .');
}
Return false;
}
}
/**
* Enter description here...
*
* @ Param mixed $ var
* @ Param mixed $ val
*/
Function assign ($ var, $ val ){
If (isset ($ var) & is_array ($ var )){
$ This-> tpl_vars = $ var;
} Else if (isset ($ var) & $ var! = ''){
$ This-> tpl_vars [$ var] = $ val;
} Else {
If ($ this-> _ debug = true ){
$ This-> _ debug ('set variable error .');
}
Return false;
}
}
/**
* Display template file
*
* @ Param String $ file_name
*/
Function display ($ file_name ){
Ob_start ();
Extract ($ this-> tpl_vars );
$ Include_flie = $ this-> tpl_path. $ file_name;
If (! File_exists ($ include_flie )){
If ($ this-> _ debug)
$ This-> _ debug ('template file "'. $ include_flie.'" is not exists .');
Else
Exit ('template error, please check it .');
}
Include ($ include_flie );
$ Content = ob_get_contents ();
Ob_end_clean ();
Echo $ content;
}
/**
* Debuging
*
*/
Function _ debug ($ msg = ''){
Die ('error: '. $ msg );
}
}
?>