output buffering callback limitation

php.internals

Okin Okin

22 years ago
Hi, I'm having trouble with output buffering limitation and the output_callback function of ob_start. look at this script : <?php function ob_perso_handler($buffer) { ob_start(); echo 'foo'; $buffer .= ob_get_contents(); ob_end_clean(); return $buffer; } ob_start('ob_perso_handler'); echo 'Hello World !'; ?> it outputs : "Fatal error: ob_start(): Cannot use output buffering in output buffering display handlers" Why then ? It would be a killer feature for my next killer appz ! I've tried also : <?php function ob_perso_handler1($buffer) { return $buffer . 'f1'; } function ob_perso_handler2($buffer) { ?> foo <? return $buffer . 'f2'; } ob_start('ob_perso_handler1'); ob_start('ob_perso_handler2'); echo 'Hello World !'; ?> but were does "foo" go ? any idea on that ? maybe some source change to PHP to permit one or the other solution ? I'm new to this mailing list, please redirect me to the correct one if I'm wrong here. My thought was it was a developper problem, since change to the source has to be done to enable such this feature ... Many thank's for any help ! Nikos Yahoo! Mail : votre e-mail personnel et gratuit qui vous suit partout ! Créez votre Yahoo! Mail sur http://fr.benefits.yahoo.com/ Dialoguez en direct avec vos amis grâce à Yahoo! Messenger !Téléchargez Yahoo! Messenger sur http://fr.messenger.yahoo.com

Taco van den Broek

22 years ago
Okin okin wrote:
> Hi, > > I'm having trouble with output buffering limitation > and the output_callback function of ob_start. > > look at this script : > > <?php > > function ob_perso_handler($buffer) > { > ob_start(); > echo 'foo'; > $buffer .= ob_get_contents(); > ob_end_clean(); > > return $buffer; > } > > ob_start('ob_perso_handler'); > > echo 'Hello World !'; > > ?> > > it outputs : "Fatal error: ob_start(): Cannot use > output buffering in output buffering display handlers" > > > Why then ? It would be a killer feature for my next > killer appz ! > > I've tried also : > > <?php > > function ob_perso_handler1($buffer) > { > return $buffer . 'f1'; > } > > function ob_perso_handler2($buffer) > { > ?> > foo > <? > > return $buffer . 'f2'; > } > > ob_start('ob_perso_handler1'); > ob_start('ob_perso_handler2'); > > echo 'Hello World !'; > > ?> > > but were does "foo" go ? > > > any idea on that ? maybe some source change to PHP to > permit one or the other solution ? > > I'm new to this mailing list, please redirect me to > the correct one if I'm wrong here. My thought was it > was a developper problem, since change to the source > has to be done to enable such this feature ... > > Many thank's for any help ! > > Nikos > > > > > > > Yahoo! Mail : votre e-mail personnel et gratuit qui vous suit partout ! > Créez votre Yahoo! Mail sur http://fr.benefits.yahoo.com/ > > Dialoguez en direct avec vos amis grâce à Yahoo! Messenger !Téléchargez Yahoo! Messenger sur http://fr.messenger.yahoo.com
I don't really understand your problem, it seams to me you want to add output to the buffer in the output handler!? Why not just: <?php function myObHandler($buffer) { $buffer .= 'foo'; return $buffer; } ob_start('myObHandler'); ?> -Taco

Okin Okin

22 years ago
One last note on this : the limitation I'm talking about is annoying in two cases for me : - I can't use "print_r($var, true)" inside an ob_handler, because print_r uses ob_start internally. And I need this to append some debug info to my buffer. The workaround for this is to make my own print_r... - In some cases, I would like to send an email from an ob_handler. But I make my emails with some logic/presentation separation. And most if not all template systems use ob_start to fetch their output into a $variable. The workaround here is to register_shutdown_function the mail-sending function inside the ob_handler. As you can see, I've two real problems, and a real solution (by PHP source mods) could be far better than two workarounds isn't it ? One would allow ob_start to be called inside an ob_handler, and the error message would be printed only if output buffering is always on at the end of this ob_handler, it would be perfect ! Please consider this idea, and thank you for your patience. Nikos Taco van den Broek <taco@procurios.nl> wrote: Okin okin wrote:
> I'm having trouble with output buffering limitation > and the output_callback function of ob_start. > > look at this script : > > function ob_perso_handler($buffer) > { > ob_start(); > echo 'foo'; > $buffer .= ob_get_contents(); > ob_end_clean(); > > return $buffer; > } > > ob_start('ob_perso_handler'); > > echo 'Hello World !'; > > ?> > > it outputs : "Fatal error: ob_start(): Cannot use > output buffering in output buffering display handlers" >
I don't really understand your problem, it seams to me you want to add output to the buffer in the output handler!? Why not just: <?php function myObHandler($buffer) { $buffer .= 'foo'; return $buffer; } ob_start('myObHandler'); ?> --------------------------------- Yahoo! Mail : votre e-mail personnel et gratuit qui vous suit partout ! Créez votre Yahoo! Mail Dialoguez en direct avec vos amis grâce à Yahoo! Messenger !

Andre Cerqueira

22 years ago
I think an ob_handler should be designed to handle output. Not create (output), nor send emails. I think the workaround is the other way around hehe It's the way you want it to work that is a workaround, since there is probably a better way to do it. -I agree, print_r($var, true) should work but doesn't. You could print_r() before or after ob_handler being called (upon script end or ob_end_flush, or something like that) -I think you could still send the email from the ob_handler (though i think it's ugly) and keep the separation if you read the template file and handle it instead of including directly after a ob_start() (that's what you're doing, right?) Or you could send the email out of the ob_handler
> One would allow ob_start to be called inside an ob_handler, and the >error message would be printed only if output buffering is always on
at >the end of this ob_handler, it would be perfect ! Not sure if i undertood you. Are you trying to emit errors during ob_handler execution and prevent them from being output using ob_start()? You could use error_reporting()+error_log()[+trigger_error()] or something? Okin okin wrote:

Okin Okin

22 years ago
>Not sure if i undertood you. Are you trying to emit >errors during >ob_handler execution and prevent them from being >output using ob_start()? >You could use
error_reporting()+error_log()[+trigger_error()] or something? Nop, I'm considering this : <?php function ob_perso_handler($buffer) { ob_start(); echo 'foo'; return $buffer . ob_get_clean(); } ob_start('ob_perso_handler'); echo 'Hello World !'; ?> this code produces "Fatal error: ob_start(): Cannot use output [...]" the idea is that the above code don't makes error anymore, but this one yes <?php function ob_perso_handler($buffer) { ob_start(); echo 'foo'; return $buffer; } ob_start('ob_perso_handler'); echo 'Hello World !'; ?> with an error message like this one : "Fatal error: each output buffer started inside an ob_handler must be destroyed before the end of this ob_handler"
>Andre Cerqueira <accerqueira@superig.com.br> wrote: I
think an ob_handler should be designed to handle
>output. >Not create (output), nor send emails. >I think the workaround is the other way around hehe >It's the way you want it to work that is a >workaround, since there is >probably a better way to do it. > > >-I agree, print_r($var, true) should work but
doesn't.
>You could print_r() before or after ob_handler being >called (upon script >end or ob_end_flush, or something like that)
>-I think you could still send the email from the >ob_handler (though i >think it's ugly) and keep the separation if you read >the template file >and handle it instead of including directly after a >ob_start() (that's >what you're doing, right?) >Or you could send the email out of the ob_handler > > >> One would allow ob_start to be called inside an >ob_handler, and the >>error message would be printed only if output >>buffering is always on >>at the end of this ob_handler, it would be perfect ! > >Not sure if i undertood you. Are you trying to emit >errors during >ob_handler execution and prevent them from being >output using ob_start()? >You could use
error_reporting()+error_log()[+trigger_error()] or something?
> > > >Okin okin wrote: > One last note on this : > > the limitation I'm talking about is annoying in two
cases for me :
> - I can't use "print_r($var, true)" inside an
ob_handler, because print_r uses ob_start internally. And I need this to append some debug info to my buffer. The workaround for this is to make my own print_r...
> > - In some cases, I would like to send an email from
an ob_handler. But I make my emails with some logic/presentation separation. And most if not all template systems use ob_start to fetch their output into a $variable. The workaround here is to register_shutdown_function the mail-sending function inside the ob_handler.
> > > Okin okin wrote: > >>I'm having trouble with output buffering limitation >>and the output_callback function of ob_start. >> >>look at this script : >> >>function ob_perso_handler($buffer) >>{ >>ob_start(); >>echo 'foo'; >>$buffer .= ob_get_contents(); >>ob_end_clean(); >> >>return $buffer; >>} >> >>ob_start('ob_perso_handler'); >> >>echo 'Hello World !'; >> >>?> >> >>it outputs : "Fatal error: ob_start(): Cannot use >>output buffering in output buffering display
handlers" Yahoo! Mail : votre e-mail personnel et gratuit qui vous suit partout ! Créez votre Yahoo! Mail sur http://fr.benefits.yahoo.com/ Dialoguez en direct avec vos amis grâce à Yahoo! Messenger !Téléchargez Yahoo! Messenger sur http://fr.messenger.yahoo.com

Andre Cerqueira

22 years ago
maybe php.general would be more appropriate im not a developer, but i think this "feature" doesnt make sense hehe i think ob display handlers are not supposed to output things directly, they just return them to the function that will output it at your first snip, you could/should do: <?php function ob_perso_handler($buffer) { $buffer = 'foo'. $buffer; //you already received output data return $buffer; } ob_start('ob_perso_handler'); echo 'Hello World !'; ?> at the second: <?php function ob_perso_handler1($buffer) { return $buffer .'f1'; } function ob_perso_handler2($buffer) { return 'foo'. $buffer .'f2'; } ob_start('ob_perso_handler1'); ob_start('ob_perso_handler2'); echo 'Hello World !'; ?> good luck on the killer appz Okin okin wrote: