473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

A general buffering function

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
8 1778
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
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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Mathias Herrmann | last post by:
Hi. I have the following problem: Using popen() to execute a program and read its stdout works usually fine. Now I try to do this with a program called xsupplicant (maybe one knows), but I dont...
6
by: Dave Veeneman | last post by:
I'm drawing a graphic directly to a form, using a Paint event handler. All works well, except that the graphic flickers when I resize the form. So, I implement double-buffering for the form using...
2
by: Jason | last post by:
I have created a 2d isometric game map using tiles and now I'm trying to move around my map..when i go near the edge of the map I want to redraw the map to show new parts of the map however the...
7
by: Philip Wagenaar | last post by:
I use a class to manage tiff's (written by someone else) to splitt multipage tiffs. However, when I run it, it fails, for other people it seems work ok. The method in the class is :public...
3
by: ssoffline | last post by:
hi i have an app in which i can drop objects onto a form and move them, it consists of graphics (lines), i am using double buffering to avoid filckering in the parent control which is a panel,but...
11
by: japhy | last post by:
Is there a way to read a line (a series of characters ending in a newline) from a file (either by descriptor or stream) atomically, without buffering additional contents of the file?
22
by: Neil Gould | last post by:
Or... when is a script not a script? I have several modules for managing different aspects of our club's website, most of which are multi-page. Does setting such things as server.ScriptTimeout...
11
by: JRough | last post by:
I'm trying to use output buffering to cheat so i can print to excel which is called later than this header(). header("Content-type: application/xmsdownload"); header("Content-Disposition:...
0
by: JRough | last post by:
On Sep 26, 3:23 pm, Captain Paralytic <paul_laut...@yahoo.comwrote: without the exit I get this again: Warning: Cannot modify header information - headers already sent by (output started at...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.