From:             sv4php at fmethod dot com
Operating system: any
PHP version:      5.2.6
PHP Bug Type:     Output Control
Bug description:  Can not set chunk size to 1 with ob_start()

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 bug report at http://bugs.php.net/?id=45391&edit=1
-- 
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=45391&r=trysnapshot52
Try a CVS snapshot (PHP 5.3): 
http://bugs.php.net/fix.php?id=45391&r=trysnapshot53
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=45391&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=45391&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=45391&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=45391&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=45391&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=45391&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=45391&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=45391&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=45391&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=45391&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=45391&r=globals
PHP 4 support discontinued:   http://bugs.php.net/fix.php?id=45391&r=php4
Daylight Savings:             http://bugs.php.net/fix.php?id=45391&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=45391&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=45391&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=45391&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=45391&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=45391&r=mysqlcfg

Reply via email to