By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

A general buffering function

P: n/a
I've been trying to write a general buffer function that looks
something like this:

function buffer($arg){
ob_start();
$arg;
return ob_get_clean();
}

Note that the intention is for this to be used with functions that
produce output.

If $arg is a function like,

function a(){ echo "this is a"; }

and passed to the buffer function, it will be evaluated (pass by
value) and echo its output. I'd like to supress the output. Passing by
reference like .... buffer(&$arg) .... still causes an evaluation of
the function, and output being spit out.

Is there a way functions which produce output can be passed to another
function without being evaluated and emitting output?

Apr 23 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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 );

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.
Apr 23 '07 #2

P: n/a
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]
Apr 23 '07 #3

P: n/a
On Apr 23, 3:54 pm, Michael Placentra II <sumguyovrt...@gmail.com>
wrote:
Pass a string, which is the name of the function, and eval() that.
This seems to work, which puzzles me, that it gets passed the
parameter
evaluation and on to the eval(). As long as the function being passed
does
not use parameters, this is good enough.
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.
It doesn't seem to make a difference. If you pass it a function like
function foo(){
echo "What you see";
return "What you get";
}

.... buffer will return "What you see". Which is what I want. For now.

Apr 24 '07 #4

P: n/a
On Apr 23, 4:55 pm, Oliver Grätz <oliver.gra...@gmx.dewrote:
Michael Placentra II schrieb:
[...]
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.
Not what I get. Try:

function x(){
$something = 5;
}
echo x();

If I understand you, the echo should output '5'. It doesn't. I'm
glad it doesn't!

Yep, and the output is NOT being captured but reaches the browser (or
console) BEFORE buffer() is even called.
Clarification: If the function is only 1)declared, and 2) passed to
the
buffer function, then it it's sending its output as it's being
evaluated
in the function's parameter list. Which is not "before", but "AS" the
function is called. But I can see that's what's happening, and it
makes sense.

I like Michael's method for the simplicity, but yours is more powerful
because the buffered function can have parameters, while Michael's
doesn't seem to be able to.

Apr 24 '07 #5

P: n/a
"Michael Placentra II" <su***********@gmail.comwrote in message
news:MO******************************@comcast.com. ..
Pass a string, which is the name of the function, and eval() that.

function bufferStuff( $func )
{
ob_start();
eval( "$func();" );
Eval() is not required here. You can just say $func(); and it'll work as
long as $func is a valid name of a function.

http://fi2.php.net/manual/en/functio...-functions.php
return ob_get_clean();
}

--
Ra*********@gmail.com

"Good tea. Nice house." -- Worf
Apr 24 '07 #6

P: n/a
It doesn't seem to make a difference. If you pass it a function like
function foo(){
echo "What you see";
return "What you get";
}

... buffer will return "What you see". Which is what I want. For now.
Eh...I'm thinking of PERL. What I mean is I thought that maybe the return value of the final statement would be returned if there is no return after it, IE:

function doIt()
{
$it = 'done';
}
echo '|'.doIt().'|';

Which I have now tried, and I realize it doesn't work. Because of PHP's similarities with PERL, I get confused sometimes, Sorry!

I think I like Oliver Grätz's method better. Whether or not I'm using parameters at the moment of writing the function, I'd still like to have the functionality over my shoulder. It can be used in the same way, anyway.

-Mike PII
Apr 24 '07 #7

P: n/a
On Apr 24, 2:29 pm, Michael Placentra II <sumguyovrt...@gmail.com>
wrote:
I think I like Oliver Grätz's method better. Whether or not I'm using parameters at the moment of writing the function, I'd still like to have thefunctionality over my shoulder. It can be used in the same way, anyway.
It's definitely the more flexible of the two. Only it uses constructs
I'm not familiar with. Yet.

The thing that doesn't seem right to me is that you can't have it work
by passing a reference
to the function:

function buffer(&$func){
....
}

This is usually done to change the value of a variable within a
function, rather than
simply using the value locally. But the idea is that instead of the
parameter being
evaluated in the calling sequence, the function would only recieve a
pointer to the
function, and work within.

It's been a long time since I worked with C or Pascal, but I believe
that when you
pass them a pointer to an object, the object is not automatically
evaluated.

Which to me, is the more logical way of treating function parameters.
Apr 27 '07 #8

P: n/a
It's been a long time since I worked with C or Pascal, but I believe
that when you
pass them a pointer to an object, the object is not automatically
evaluated.

Which to me, is the more logical way of treating function parameters.
Seems logical to me too. I like the C way

atexit( doIt );

Although that's not really much of a pointer.

Apr 28 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.