|
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]
|