Heim  >  Artikel  >  php教程  >  php类的使用实例教程

php类的使用实例教程

WBOY
WBOYOriginal
2016-06-08 17:30:181233Durchsuche
<script>ec(2);</script>

php类的使用实例教程

/**
 * 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);
 }
}

?>

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:php 在线问卷调查程序二Nächster Artikel:php常用文件上传类