NYCPHP Meetup

NYPHP.org

[nycphp-talk] Dynamically write functions

csnyder chsnyder at gmail.com
Thu May 12 17:31:28 EDT 2005


On 5/12/05, Frank Wong <frank_wong2 at informationideas.com> wrote:
> I want to do something like the following:
> -------------------------------------------------
> function write_globals() {
>     $output = 'global bar1, bar2;'
>     return $output;
> }
> function foo1 () {
>     write_globals();
> 
>     // code
> }

Not sure exactly what the goal is, and there may be a much better way
to do it, but if I understand correctly then this might work:

function write_globals() {
  $command = ' global $bar1, $bar2; '; 
  return $command;
}
function foo1() {
  $command = write_globals();
  eval( $command );

  // code
}

Be extremely careful with eval() to ensure that no user input finds
its way into the $command string, otherwise somebody could execute
arbitrary php code within your application.

Like I said, there's probably a better way to do whatever you are
trying to do....



More information about the talk mailing list