You are here: Re: A general buffering function « PHP Programming Language « IT news, forums, messages
Re: A general buffering function

Posted by Oliver Grδtz on 04/23/07 22:55

Michael Placentra II schrieb:
> Pass a string, which is the name of the function, and eval() that.
>
> function bufferStuff( $func )
> {
> ob_start();
> eval( "$func();" );
> return ob_get_clean();
> }
>
> function outputStuff()
> {
> echo "foo and bar\nand foo";
> }
>
> $stuff = bufferStuff( 'outputStuff' );
>
> echo substr( $stuff, 0, 11 );

This is a bit limited, since you can only call simple functions without
parameters. Use a callback!

function please_use_a_prefix_buffer()
{
$args = func_get_args();
$callback = array_shift($args);
ob_start();
call_user_func_array($callback,$args);
return ob_get_clean();
}

$text = buffer(array('classname','methodname'),$par1,$par2);

And this one even avoids the use of eval()...

> Note that if you are doing this with your current function (and my
> output function):
>
> buffer( outputStuff() );
>
> Then the function outputStuff() is not being passed to your function,
> rather it is being run and then the return value of outputStuff() is
> being passed to buffer(). I believe that in PHP if you don't use the
> return keyword in a function, the return value of the last statement in
> the function is returned, so what is really being passed to your
> buffer() function is the return value of the last statement in the
> function you are trying to pass to it, which may actually be the same
> string that is being outputted.

Yep, and the output is NOT being captured but reaches the browser (or
console) BEFORE buffer() is even called.

OLLi

--
Dr. Goldfine: "So you agreed to marry him just to be polite?"
Bree: "That's the downside of having good manners."
[DH 207]

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация