pass arbitrary vars into a func from another func? [message #177517] |
Wed, 04 April 2012 17:52 |
J. Frank Parnell
Messages: 12 Registered: January 2012
Karma: 0
|
Junior Member |
|
|
In wordpress and probly other software, there are functions you can
use to echo stuff into your page. just call
comments_number('0','1','%');
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
number.
so, something like
ob_start();
comments_number('0','1','%');
$commentsnum = ob_get_contents();
ob_end_clean();
works.
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()){
ob_start();
$func($args);
$ret= ob_get_contents();
ob_end_clean();
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()
function.
|
|
|
Re: pass arbitrary vars into a func from another func? [message #177518 is a reply to message #177517] |
Wed, 04 April 2012 20:58 |
Thomas Mlynarczyk
Messages: 131 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
J. Frank Parnell schrieb:
> function echoToVar($func,$args = array()){
> ob_start();
> $func($args);
> $ret= ob_get_contents();
> ob_end_clean();
> return $ret;
> }
>
> but i'm not sure how to get the args into the variable func.
You want <www.php.net/call-user-func-array>:
function echoToVar( $func, array $args = array() )
{
ob_start();
call_user_func_array( $func, $args );
$ret = ob_get_contents();
ob_end_clean();
return $ret;
}
Greetings,
Thomas
--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
|
|
|
Re: pass arbitrary vars into a func from another func? [message #177523 is a reply to message #177518] |
Thu, 05 April 2012 20:14 |
J. Frank Parnell
Messages: 12 Registered: January 2012
Karma: 0
|
Junior Member |
|
|
Great! thanks :)
On Apr 4, 1:58 pm, Thomas Mlynarczyk <tho...@mlynarczyk-webdesign.de>
wrote:
> J. Frank Parnell schrieb:
>
>> function echoToVar($func,$args = array()){
>> ob_start();
>> $func($args);
>> $ret= ob_get_contents();
>> ob_end_clean();
>> return $ret;
>> }
>
>> but i'm not sure how to get the args into the variable func.
>
> You want <www.php.net/call-user-func-array>:
>
> function echoToVar( $func, array $args = array() )
> {
> ob_start();
> call_user_func_array( $func, $args );
> $ret = ob_get_contents();
> ob_end_clean();
> return $ret;
>
> }
>
> Greetings,
> Thomas
>
> --
> Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
> (Coluche)
|
|
|
Re: pass arbitrary vars into a func from another func? [message #177524 is a reply to message #177518] |
Fri, 06 April 2012 09:51 |
Erwin Moller
Messages: 228 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 4/4/2012 10:58 PM, Thomas Mlynarczyk wrote:
> J. Frank Parnell schrieb:
>
>> function echoToVar($func,$args = array()){
>> ob_start();
>> $func($args);
>> $ret= ob_get_contents();
>> ob_end_clean();
>> return $ret;
>> }
>>
>> but i'm not sure how to get the args into the variable func.
>
> You want <www.php.net/call-user-func-array>:
>
> function echoToVar( $func, array $args = array() )
> {
> ob_start();
> call_user_func_array( $func, $args );
> $ret = ob_get_contents();
> ob_end_clean();
> return $ret;
> }
>
> Greetings,
> Thomas
>
A little warning to Mr. Parnell, the original poster, concerning
call_user_func_array:
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.
Regards,
Erwin Moller
--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
|
|
|