-
Notifications
You must be signed in to change notification settings - Fork 1
OutputBuffering
This helper makes working with output buffering easier: The
return values of all output buffering methods can return a
boolean false
, which must be checked each time.
As a result, the following is not safe:
function renderContent() : string
{
ob_start();
echo 'content';
return ob_get_clean(); // can return false
}
Even if it is unlikely to fail, it is still bad practice.
The helper solves this by doing these checks for you, and handling them with exceptions.
The OutputBuffering
class is static, and can be simply
used like this:
use \AppUtils\OutputBuffering;
function renderContent() : string
{
OutputBuffering::start();
echo 'Content';
return OutputBuffering::get();
}
Compared to the PHP functions, all methods of the helper assume
you wish to stop the buffering. As a result, the methods get()
and flush()
automatically stop the buffering.
If you wish to use the more advanced features of the output buffering, you will have to use the native functions.
Fetching the content of the buffer into a variable.
use \AppUtils\OutputBuffering;
OutputBuffering::start();
echo 'Content';
$content = OutputBuffering::get();
Flush the content of the buffer to standard output.
use \AppUtils\OutputBuffering;
OutputBuffering::start();
echo 'Content';
OutputBuffering::flush();
Stop the buffer, without using its contents.
use \AppUtils\OutputBuffering;
OutputBuffering::start();
echo 'Content';
OutputBuffering::stop();
The helper uses the native PHP methods, but all checks of the buffering level and whether the buffering is active are done separately from the native PHP methods.
In practice, this means that calling isActive()
will return
false
if output buffering has not been started by the helper.
Only calls made with the helper are tracked.
This means that you can safely mix native calls with the helper's methods.
The helper will do some basic error checking, like trying to stop the buffer if it has not been started. However, it cannot check if a buffer has not been closed.
For this, your application must implement its own checks, because
only it knows when a request is completed. It can use the method
isActive()
to check if buffering is still active, and act
accordingly.
One way to verify if all buffers have been stopped correctly is to do this during shutdown, and store a log message (since exceptions cannot be thrown at shutdown). This can then be evaluated later to identify where in the application the buffers are not stopped correctly.
register_shutdown_function('checkBuffers');
function checkBuffers() : void
{
if(OutputBuffering::isActive())
{
// Not all buffers are stopped.
// Log this somewhere
}
}
This can also be used to check the native PHP buffering.
New here?
Have a look at the overview for a list of all helper classes available in the package.
Table of contents
Find the current page in the collapsible "Pages" list above, and expand the page, to view a table of contents.