Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #5345 > unrolled thread

pass arbitrary vars into a func from another func?

Started by"J. Frank Parnell" <juglesh@gmail.com>
First post2012-04-04 10:52 -0700
Last post2012-04-06 11:51 +0200
Articles 4 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  pass arbitrary vars into a func from another func? "J. Frank Parnell" <juglesh@gmail.com> - 2012-04-04 10:52 -0700
    Re: pass arbitrary vars into a func from another func? Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2012-04-04 22:58 +0200
      Re: pass arbitrary vars into a func from another func? "J. Frank Parnell" <juglesh@gmail.com> - 2012-04-05 13:14 -0700
      Re: pass arbitrary vars into a func from another func? Erwin Moller <erwinmollerusenet@xs4all.nl> - 2012-04-06 11:51 +0200

#5345 — pass arbitrary vars into a func from another func?

From"J. Frank Parnell" <juglesh@gmail.com>
Date2012-04-04 10:52 -0700
Subjectpass arbitrary vars into a func from another func?
Message-ID<29aa0c10-43ad-4b6e-9d4d-34e84b89a1ba@h4g2000pbe.googlegroups.com>
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.

[toc] | [next] | [standalone]


#5346

FromThomas Mlynarczyk <thomas@mlynarczyk-webdesign.de>
Date2012-04-04 22:58 +0200
Message-ID<jlicla$vid$1@news.albasani.net>
In reply to#5345
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)

[toc] | [prev] | [next] | [standalone]


#5351

From"J. Frank Parnell" <juglesh@gmail.com>
Date2012-04-05 13:14 -0700
Message-ID<66282465-488a-4604-9589-9a45a6627f69@9g2000pbn.googlegroups.com>
In reply to#5346
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)

[toc] | [prev] | [next] | [standalone]


#5352

FromErwin Moller <erwinmollerusenet@xs4all.nl>
Date2012-04-06 11:51 +0200
Message-ID<4f7ebc9e$0$6854$e4fe514c@news2.news.xs4all.nl>
In reply to#5346
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web