php缓冲区探析

来源:互联网 发布:淘宝达人佣金给多少 编辑:程序博客网 时间:2024/06/08 00:43

缓冲区用到的3个函数:

1.ob_start();
2.$buffer = ob_get_contents();
3.ob_end_clean();

用法示例:CI框架 system/core/Exceptions.php   Ci_Exceptions 的show_error方法

/**
 * General Error Page
 *
 * Takes an error message as input (either as a string or an array)
 * and displays it using the specified template.
 *
 * @param  string    $heading   Page heading
 * @param  string|string[]    $message   Error message
 * @param  string    $template  Template name
 * @param  int       $status_code   (default: 500)
 *
 * @return string Error page output
 */
public function show_error($heading, $message, $template = 'error_general', $status_code = 500)
{
   $templates_path = config_item('error_views_path');
   if (empty($templates_path))
   {
      $templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR;
   }


   if (is_cli())
   {
      $message = "\t".(is_array($message) ? implode("\n\t", $message) : $message);
      $template = 'cli'.DIRECTORY_SEPARATOR.$template;
   }
   else
   {
      set_status_header($status_code);
      $message = '<p>'.(is_array($message) ? implode('</p><p>', $message) : $message).'</p>';
      $template = 'html'.DIRECTORY_SEPARATOR.$template;
   }


   if (ob_get_level() > $this->ob_level + 1)
   {
      ob_end_flush();
   }
   ob_start();
   include($templates_path.$template.'.php');
   $buffer = ob_get_contents();
   ob_end_clean();
   return $buffer;
}


未完待续。。。。。。。

原创粉丝点击