Php-PHP source code

Source: Internet
Author: User
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 );
}
}

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.