p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Functions as parameters to Functions?? How would you do that?


Message #1 by "Dan Ostrowski" <dan@t...> on Tue, 2 Apr 2002 20:40:40
Hello all..

What I am wondering is if you can pass a function as a parameter to a 
function...

What I am wanting to do, for example, is create a function that will make 
a table and fill the outermost cells with border graphics and spacer 
graphics, and then take a parameter to fill in the middle table...

Then I could possibly pass that function differing functions to write out 
stuff inside...

/* this kind of function could be the 'wrapper' function */

function wrapper(input?) {
    echo "<table><tr><td><img src="top_left.gif"></td>";
    echo "<td><img src="top_middle.gif"></td>";
    echo "<td><img src="top_right.gif"></td></tr>";
    echo "<tr><td><img src="middle_left.gif"></td>";
    input
    echo "<td><img src="middle_left.gif"></td></tr>";
    etc. etc. etc.
}


/* this could be any number of functions that are passed to the wrapper 
function */

function fill_in_news() {
    db_connect();
    // get news
    // print out news headers and stories, etc.
}


and then somehow call this?

wrapper(fill_in_news());


/boggle


if you can help, thanks.
dan
Message #2 by "Nikolai Devereaux" <yomama@u...> on Tue, 2 Apr 2002 11:46:38 -0800
Pass the name of the function as a string.  A lot of php builtins already do
this; usort comes to mind.

<?

function say_hello()
{
   echo "Hello";
}


function do_func($function)
{
   $function();
}


do_func("say_hello");

?>


This is one of the cool features of PHP which, like variable variables
($$var), can be easily misused... so be careful. =)

It comes in really handy in situations like usort or foreach where you want
to apply a function across a set of values.

I haven't ever had to use it myself, but I would think it'd be possible to
either pass an array of parameters (default to null) or use the
func_num_args, func_get_arg[s] functions to be able to pass parameters to
your functions as well.


HTH,

nik

Message #3 by "Dan Ostrowski" <dan@t...> on Tue, 2 Apr 2002 21:24:18
excellent!  I will try that.

As always, thanks a lot Nik!

(btw, what do you mean "mis-used?"  is the exampe of what i wanted to do a 
bad way to use it?  It seemed to me that it would be a great way to wrap 
content if you needed/wanted to use messy graphics borders...


dan
Message #4 by "Nikolai Devereaux" <yomama@u...> on Tue, 2 Apr 2002 14:11:57 -0800
> (btw, what do you mean "mis-used?"  is the exampe of what i
> wanted to do a
> bad way to use it?  It seemed to me that it would be a great way to wrap
> content if you needed/wanted to use messy graphics borders...

Well, what I mean by misused is that sometimes KNOWING you can a variable's
value as a var or function name leads them to think that they should.

Like creating variables from user input.  Bad idea.  Cool concept, bad idea.

Your method should work fine because you're reusing code but producing
different results... this is great because if there's an error anywhere, it
will appear everywhere (easier to find), and will be located in one place
(easier to fix).

I think it's the order in which the knowledge is aquired -- You came up with
a possible solution to a problem, and found out if that solution is
possible.  That's a Good Thing.

The other way is when someone finds out that the solution is possible, and
try to force that particular solution onto a problem that may not be best
suited for it.


Take care,

Nik


  Return to Index