Req #45391 [Opn->Wfx]: Can not set chunk size to 1 with ob_start()
Edit report at https://bugs.php.net/bug.php?id=45391&edit=1
ID: 45391
Updated by: cmb@php.net
Reported by: sv4php at fmethod dot com
Summary: Can not set chunk size to 1 with ob_start()
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: Output Control
Operating System: *
PHP Version: 5.2.6
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
That looks like as terrible use case to me, and given there have
been no comments for more than ten years, I'm closing as WONTFIX.
Previous Comments:
------------------------------------------------------------------------
[2008-06-29 13:09:16] sv4php at fmethod dot com
Description:
------------
chunk_size in ob_start() has no way to be specified to "1 byte" because 1 was aliased to
4096.
I understand this was done since it was deemed there's no use case for calling the handler
immediately after echo, but there is.
I need my handler to receive every item being outputted exactly after it was outputted exactly as it
is, i.e. no buffering.
This is very useful for "deferred dynamic composition", for example batch "string
id" replace in international apps, without manually searching for "macros" with
str_replace or regular expression (cleaner and more secure).
Example below of this technique (I use chunk_size = 2, to demonstrate, but if the string id-s are
one symbol apart, it'll break my example as I can't specify 1 byte in chunk_size, which is
the gut of the problem.)
I suggest one of two solutions:
- Make "1" be "1", and leave the default value (which most people use) as 4096
bytes.
- To preserve full BC, leave "1" to be "4096" but add a new alias,
"-1" to mean "no buffering" (i.e. 1 byte chunk_size). Ugly, but BC.
Reproduce code:
---------------
<?php
// stores all parts of the output, static via the handler, dynamic via translateStringId()
$composition = array();
function handler($input) {
global $composition;
$composition[] = $input;
return ''; // discard output (just saves a bit of RAM)
}
function echoStringId($stringId)
{
global $composition;
$composition[] = array($stringId); // we chose array wrapped string to mean "string id to be
resolved later";
}
function batchReplaceStringIds() {
global $composition;
// a mock of a SELECT query that would fetch all string ids at once (much faster than one query per
string).
$mockQuery = array(
'stringid1' => 'Foo',
'stringid2' => 'Bar',
);
foreach ($composition as & $compositionItem) {
if (is_array($compositionItem)) {
$compositionItem = $mockQuery[$compositionItem[0]];
}
}
}
// THE PAGE BEGINS:
ob_start('handler', 2);
?>
This is string one: <? echoStringId('stringid1'); ?>. And, this is string two: <?
echoStringId('stringid2'); ?>.
<?
ob_end_clean();
// PAGE ENDS. PROCESSING AND OUTPUT:
batchReplaceStringIds();
// echo the computed output, outputs: This is string one: Foo. And, this is string two: Bar.
foreach ($composition as $compositionItem) echo $compositionItem;
?>
Expected result:
----------------
Expected: being able to use handlers without buffering.
Actual result:
--------------
Actual: unable to turn off buffering.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=45391&edit=1
Thread (2 messages)