pass arbitrary vars into a func from another func?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
In wordpress and probly other software, there are functions you can
use to echo stuff into your page.  just call
and it echo the number of comments for the post.  The problem is, you
cant use that to set up, eg, a link to be echo'd later in your page,
because it will echo it when you call it.

$var = comments_number('0','1','%');
wont work to put the number into your $var cuz it will just echo the

so, something like
$commentsnum = ob_get_contents();

now i could wrap funcs into other funcs that would do that, but i was
wondering about a generic func i could pass the func name and an array
of vars that the func expected

function echoToVar($func,$args = array()){
             $ret= ob_get_contents();
    return $ret;

but i'm not sure how to get the args into the variable func.  Some
funcs that are expecting an array would work with what i have above,
but i dont see how i could use this with eg the comments_number()

Re: pass arbitrary vars into a func from another func?

J. Frank Parnell schrieb:

Quoted text here. Click to load it

You want <>:

function echoToVar( $func, array $args = array() )
     call_user_func_array( $func, $args );
     $ret = ob_get_contents();
     return $ret;


Ce n'est pas parce qu'ils sont nombreux avoir tort qu'ils ont raison!

Re: pass arbitrary vars into a func from another func?

Great! thanks :)

Quoted text here. Click to load it

Re: pass arbitrary vars into a func from another func?

On 4/4/2012 10:58 PM, Thomas Mlynarczyk wrote:
Quoted text here. Click to load it

A little warning to Mr. Parnell, the original poster, concerning

The above solution is good, but be sure you always know where your data
comes from, in this case the value for $func and $args.

If they MIGHT come from an user (via the web, or some other means), and
the values are unchecked, they might as well contain functionnames and
arguments that might cause trouble in your application.

If somebody knows anything about your software, or guesses some
functionnames, they might cause harm if their commands can be executed
without check via call_user_func_array().

In that sense call_user_func_array() suffers the same problems as
eval(), but to a lesser extend.

In case you (the programmer) is always delivering the values for the
functionnames, don't worry.
In case you have reason to distrust, take additional measures, like a
whitelist of functions.

Erwin Moller

"That which can be asserted without evidence, can be dismissed without
-- Christopher Hitchens

Site Timeline