why use call_user_func()

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

why use call_user_func()

Post by Live24x7 »

wondering why php provides call_user_func()

why would somebody use:

$returnVal = call_user_func("myFunction");

instead of :

$returnVal = myFunction();

any special cases where call_user_func() may be more useful ?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: why use call_user_func()

Post by Christopher »

There are cases where you have the name of the function in a variable and need a way to call it instead of using $$functionname. Also there is a related function that allows to the pass the function parameters as an array which I have found handy on occasions.
(#10850)
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: why use call_user_func()

Post by jraede »

Also some methods require variables to be passed as a reference. You can get around that by creating an array with the referenced variables and then using call_user_func($method, $array_of_referenced_variables);
johngill2810
Forum Newbie
Posts: 6
Joined: Tue May 14, 2013 9:52 am

Re: why use call_user_func()

Post by johngill2810 »

call_user_func is for calling functions whose name you don't know ahead of time but it is much less efficient since the program has to lookup the function at runtime.
annaharris
Forum Commoner
Posts: 30
Joined: Mon Mar 25, 2013 6:52 am

Re: why use call_user_func()

Post by annaharris »

Check out given link to get background Image in HTML Mail. http://blog.mailermailer.com/email-desi ... aked-truth
Post Reply