什么是缓冲?
说到缓冲,也就是buffer,这里必须要和缓存做一下比较,单纯地比较定义是无意义的,莫不如看看它们做什么。缓存解决的是如何快速查找利用数据,节省cpu消耗问题,而缓冲解决的是高速cpu与低速I/O设备不匹配的问题
PHP缓冲
ob函数,ob是output_buffering的简写。既然ob函数是php扩展函数,那么ob函数主要操作的也就是php buffer了
echo print_r
函数输出的数据是怎么到达浏览器让用户看到的呢?实际上的历程是这样的:
echo、print_r => php output_buffering => webServer buffer => browser buffer => browser display
未使用ob函数时缓冲区的使用情况
我们的代码很多时候是根本不使用ob函数的,那么它们使用缓冲区了吗?这要看php设置情况。缓冲区是通过php.ini
中的output_buffering
变量控制的。其默认值是off,可以设置为on来打开buffer。
打来buffer后,即便程序中没有用ob函数,实际上代码也是使用了缓冲区的。另外,不管php.ini
中output_buffering
的设置,cli
模式下的php始终默认是关闭的
为什么要是缓冲区呢?
简单来说,高速的cpu早早处理完自己的数据,想通过线路传递给用户,但是线路太窄了,一下输送不过去。如果引入缓冲区,cpu可以将快速将生成的数据放入缓冲区,然后自己哪儿凉快儿哪儿呆着这歇着去了。缓冲区根据指令适时将数据输出。这个样就合理解决了高速cpu与低速I/O设备的矛盾了
缓冲区的数据什么时候输出呢?
- 当缓冲区满了的时候,缓冲是有容量大小的,到达极限则会自动输出内容。
- 脚本执行完毕。很多小程序输出内容没那么多,总不能等到缓冲区满了再输出吧~这一点再自然不过
使用ob函数时缓冲区的使用情况
- ob_start()
打开输出缓冲。这个函数是我们调用最多的一个函数之一。在output_buffering
设置为on
或者x k
的情况下,这个函数与其说是打开输出缓冲,还不如说将输出缓冲扩充到很大。
当然在output_buffering
设置为off
的条件下,ob_start
会起到打开buffer
的作用。ob_start()
还可以传递一个可选参数 output_callback
函数
- ob_get_contents()
只是得到输出缓冲区的内容,但不清除它
- ob_end_clean()与ob_clean()
这两个函数从字面意思上就可以看出其区别。前者清除缓冲区内容并且关闭,后者仅仅是做清除工作。需要注意的是,使用了这两个函数,在前面使用了echo
、print_r
等函数不会输出内容
- ob_flush()与flush()
ob_flush()
送出缓冲区的内容并且丢弃内容。因而在此函数之前最好采用ob_get_contents()
获得缓冲区内容。flush()
刷出服务器端缓冲,并且发往客户端。因而从流程上来说,应该是先调用ob_flush()
而后再调用flush
函数
- ob_end_flush
这个函数将送出最顶层缓冲区的内容(如果里边有内容的话),并关闭缓冲区。如果想进一步处理缓冲区中的内容,必须在ob_end_flush()
之前调用 ob_get_contents()
,因为在调用ob_end_flush()
后缓冲区内容被丢弃
- ob_get_clean()
如果你已经熟练掌握ob_get_contents()
和ob_end_clean()
,那这个函数就很简单了。因为它是前两者的结合体。它主要是得到当前缓冲区的内容并删除当前输出缓冲区
一些框架的应用
- CodeIgniter3
system/core/Loader.php
/**
* Internal CI Data Loader
*
* Used to load views and files.
*
* Variables are prefixed with _ci_ to avoid symbol collision with
* variables made available to view files.
*
* @used-by CI_Loader::view()
* @used-by CI_Loader::file()
* @param array $_ci_data Data to load
* @return object
*/
protected function _ci_load($_ci_data)
{
// Set the default data variables
foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
{
$$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
}
$file_exists = FALSE;
// Set the path to the requested file
if (is_string($_ci_path) && $_ci_path !== '')
{
$_ci_x = explode('/', $_ci_path);
$_ci_file = end($_ci_x);
}
else
{
$_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
$_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;
foreach ($this->_ci_view_paths as $_ci_view_file => $cascade)
{
if (file_exists($_ci_view_file.$_ci_file))
{
$_ci_path = $_ci_view_file.$_ci_file;
$file_exists = TRUE;
break;
}
if ( ! $cascade)
{
break;
}
}
}
if ( ! $file_exists && ! file_exists($_ci_path))
{
show_error('Unable to load the requested file: '.$_ci_file);
}
// This allows anything loaded using $this->load (views, files, etc.)
// to become accessible from within the Controller and Model functions.
$_ci_CI =& get_instance();
foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
{
if ( ! isset($this->$_ci_key))
{
$this->$_ci_key =& $_ci_CI->$_ci_key;
}
}
/*
* Extract and cache variables
*
* You can either set variables using the dedicated $this->load->vars()
* function or via the second parameter of this function. We'll merge
* the two types and cache them so that views that are embedded within
* other views can have access to these variables.
*/
empty($_ci_vars) OR $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
extract($this->_ci_cached_vars);
/*
* Buffer the output
*
* We buffer the output for two reasons:
* 1. Speed. You get a significant speed boost.
* 2. So that the final rendered template can be post-processed by
* the output class. Why do we need post processing? For one thing,
* in order to show the elapsed page load time. Unless we can
* intercept the content right before it's sent to the browser and
* then stop the timer it won't be accurate.
*/
ob_start();
// If the PHP installation does not support short tags we'll
// do a little string replacement, changing the short tags
// to standard PHP echo statements.
if ( ! is_php('5.4') && ! ini_get('short_open_tag') && config_item('rewrite_short_tags') === TRUE)
{
echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
}
else
{
include($_ci_path); // include() vs include_once() allows for multiple views with the same name
}
log_message('info', 'File loaded: '.$_ci_path);
// Return the file data if requested
if ($_ci_return === TRUE)
{
$buffer = ob_get_contents();
@ob_end_clean();
return $buffer;
}
/*
* Flush the buffer... or buff the flusher?
*
* In order to permit views to be nested within
* other views, we need to flush the content back out whenever
* we are beyond the first level of output buffering so that
* it can be seen and included properly by the first included
* template and any subsequent ones. Oy!
*/
if (ob_get_level() > $this->_ci_ob_level + 1)
{
ob_end_flush();
}
else
{
$_ci_CI->output->append_output(ob_get_contents());
@ob_end_clean();
}
return $this;
}
- Laravel
src/Illuminate/View/Engines/PhpEngine.php
/**
* Get the evaluated contents of the view at the given path.
*
* @param string $path
* @param array $data
* @return string
*/
protected function evaluatePath($path, $data)
{
$obLevel = ob_get_level();
ob_start();
// We'll evaluate the contents of the view inside a try/catch block so we can
// flush out any stray output that might get out before an error occurs or
// an exception is thrown. This prevents any partial views from leaking.
try {
$this->files->getRequire($path, $data);
} catch (Throwable $e) {
$this->handleViewException($e, $obLevel);
}
return ltrim(ob_get_clean());
}
/**
* Handle a view exception.
*
* @param \Throwable $e
* @param int $obLevel
* @return void
*
* @throws \Throwable
*/
protected function handleViewException(Throwable $e, $obLevel)
{
while (ob_get_level() > $obLevel) {
ob_end_clean();
}
throw $e;
}