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

call_user_func_array vs $myfunction( &$params )

P: n/a

Hi,

I was looking at
http://www.php.net/manual/en/functio...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).

Aug 1 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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 ;) http://code.google.com/p/pihipi
Aug 1 '07 #2

P: n/a
Rik
On Wed, 01 Aug 2007 11:04:23 +0200, gosha bine <st********@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
Aug 1 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.