p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Pointer to function?


Message #1 by jacob-o-hansen@p... on Sun, 1 Sep 2002 16:48:11
Oh! I have a problem. I am converting a site from ASP to PHP, and I do not 
know if it is possible to make a pointer to function in PHP.

I want to make a general function that makes a user interface, and I want 
to call this function with different functions as arguments, so that I can 
get the same look of all my UIs. Something like...

/* --------------------------------------------------------------- */

function someSpecial()
{
   echo "<INPUT TYPE='text' NAME='something'>";
}

function generalFunction($afunction)
{
   echo "<FORM>";
   echo "<TABLE><TR><TD>";

   /* at this place I want to call the function given as argument! */

   echo "</TD><TR></TABLE>";
   echo "</FORM>";

}

generalFunction(someSpecial());

/* --------------------------------------------------------------- */

Is this possible in some way, or rather what has to be changed...!?

thanks Jacob.
Message #2 by "JOH" <jacob-o-hansen@p...> on Mon, 2 Sep 2002 16:28:58
I have found the answer myself, if anybody is interested... I have used 
the function...

<?php call_user_func($uifunction); ?>

That is, the code will have to look like this...

/* --------------------------------------------------------------- */

function someSpecial()
{ 
    echo "<INPUT TYPE='text' NAME='something'>";
} 

function generalFunction($afunction)
{ 
   echo "<FORM>";
   echo "<TABLE><TR><TD>";
   call_user_func($afunction);
   echo "</TD><TR></TABLE>";
   echo "</FORM>";
}

generalFunction('someSpecial');

/* --------------------------------------------------------------- */

Well this should work!

Regards

Jacob.


> Oh! I have a problem. I am converting a site from ASP to PHP, and I do 
not 
k> now if it is possible to make a pointer to function in PHP.

> I want to make a general function that makes a user interface, and I 
want 
t> o call this function with different functions as arguments, so that I 
can 
g> et the same look of all my UIs. Something like...

> /* --------------------------------------------------------------- */

> function someSpecial()
{> 
 >   echo "<INPUT TYPE='text' NAME='something'>";
}> 

> function generalFunction($afunction)
{> 
 >   echo "<FORM>";
 >   echo "<TABLE><TR><TD>";

>    /* at this place I want to call the function given as argument! */

>    echo "</TD><TR></TABLE>";
 >   echo "</FORM>";

> }

> generalFunction(someSpecial());

> /* --------------------------------------------------------------- */

> Is this possible in some way, or rather what has to be changed...!?

> thanks Jacob.
Message #3 by "Dan Ostrowski" <dan@t...> on Mon, 2 Sep 2002 23:01:56
Actually you can do it this way as well...


function printstuff1 ()
{
   echo 'something inside';
}



function printstuff2($pass)
{
   echo 'outside stuff';

   $pass();
}

/**************************/
// called as ...


printstuff2('printstuff1');


Just another cool thing that PHP does for us. ^_^

regards,
dan
Message #4 by "Nikolai Devereaux" <yomama@u...> on Wed, 4 Sep 2002 11:07:13 -0700
You don't need to use the call_user_func().  You can do this, too:

function foo()
{
   echo "hello, world.";
}


$func_name = 'foo';

$func_name();


hth,

nik


  Return to Index