SearchSearch   

call_user_func_array vs $myfunction( &$params )

 
   Webmaster Forums (Home) -> PHP RSS
Next:  Uploading via PHP  
Author Message
FFMG

External


Since: Aug 01, 2007
Posts: 1



(Msg. 1) Posted: Wed Aug 01, 2007 4:11 am
Post subject: call_user_func_array vs $myfunction( &$params )
Archived from groups: comp>lang>php (more info?)

Hi,

I was looking at
http://www.php.net/manual/en/function.call-user-func-array.php and I
was wondering...

Given,
// --
function foo( &$params)
{
echo 'bar';
}
// --

What is the difference between

// --
$params = array( $stuff);
$myFn = 'foo';
$myFn($params)
// --

And

// --
$params = array( $stuff);
$myFn = 'foo';
call_user_func_array( $myFn, $params );
// --

Wont they both achieve the same result?

FFMG


--

'webmaster forum' (http://www.httppoint.com) | 'Free Blogs'
(http://www.journalhome.com/) | 'webmaster Directory'
(http://www.webhostshunter.com/)
'Recreation Vehicle insurance'
(http://www.insurance-owl.com/other/car_rec.php) | 'Free URL
redirection service' (http://urlkick.com/)
------------------------------------------------------------------------
FFMG's Profile: http://www.httppoint.com/member.php?userid=580
View this thread: http://www.httppoint.com/showthread.php?t=18946

Message Posted via the webmaster forum http://www.httppoint.com, (Ad revenue sharing).
Back to top
gosha bine

External


Since: Apr 27, 2007
Posts: 207



(Msg. 2) Posted: Wed Aug 01, 2007 11:04 am
Post subject: Re: call_user_func_array vs $myfunction( &$params )
Archived from groups: per prev. post (more info?)

On 01.08.2007 10:11 FFMG wrote:

> What is the difference between
>
> // --
> $params = array( $stuff);
> $myFn = 'foo';
> $myFn($params)
> // --
>
> And
>
> // --
> $params = array( $stuff);
> $myFn = 'foo';
> call_user_func_array( $myFn, $params );
> // --
>
> Wont they both achieve the same result?
>

call_user_func_array is useful when the number of parameters to a called
function is variable or unknown, for example:

function nice_dump() {
$params = func_get_args();
echo "<pre>";
call_user_func_array('var_dump', $params);
echo "</pre>";
}

nice_dump(1, 2, 3);



--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right Wink http://code.google.com/p/pihipi
Back to top
Rik

External


Since: Aug 08, 2006
Posts: 876



(Msg. 3) Posted: Wed Aug 01, 2007 2:32 pm
Post subject: Re: call_user_func_array vs $myfunction( &$params )
Archived from groups: per prev. post (more info?)

On Wed, 01 Aug 2007 11:04:23 +0200, gosha bine <stereofrog.DeleteThis@gmail.com>
wrote:

> On 01.08.2007 10:11 FFMG wrote:
>
>> What is the difference between
>> // -- $params = array( $stuff);
>> $myFn = 'foo';
>> $myFn($params)
>> // -- And // -- $params = array( $stuff);
>> $myFn = 'foo';
>> call_user_func_array( $myFn, $params );
>> // -- Wont they both achieve the same result?
>>
>
> call_user_func_array is useful when the number of parameters to a called
> function is variable or unknown, for example:
>
> function nice_dump() {
> $params = func_get_args();
> echo "<pre>";
> call_user_func_array('var_dump', $params);
> echo "</pre>";
> }
>
> nice_dump(1, 2, 3);

Indeed, it's not really function name that's interesting in this
particular example, it's the parameters that particular piece of code
doesn't have to worry about.

Then again, one of it's major advantages is it also supports callbacks, so
you can easily give a class function or object method to it. Your code
would be rather complex to get that otherwise...
--
Rik Wasmus
Back to top
Display posts from previous:   
       Webmaster Forums (Home) -> PHP
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum