NYCPHP Meetup

NYPHP.org

[nycphp-talk] Dynamically write functions

Rolan Yang rolan at omnistep.com
Fri May 13 00:23:57 EDT 2005


I remember doing something like:

function write_globals($bar) {
global $$bar;
print "The value of $bar is $$bar";
}

put the name of the variable into $bar and pass it to the function.


~Rolan


Frank Wong wrote:

> csnyder wrote:
>
>> 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:
>>
>>  
>>
> Well, what I have are some global variables that is set in a global 
> include file.  I need to use them within my functions.  The problem I 
> have been faced with is that once in a while my code changes require a 
> new global variable.  I find myself going into every function to add 
> 'global $bar1;'.  My thinking was that if I had a function that wrote 
> all my global vars, the next time a new global is introducted all I 
> have to do is modify the write_globals() function.
>
>> function write_globals() {
>>  $command = ' global $bar1, $bar2; ';  return $command;
>> }
>> function foo1() {
>>  $command = write_globals();
>>  eval( $command );
>>
>>  // code
>> }
>>
>>  
>>
> Aha, eval is what I was missing.  I will try that.  Thanks.
>
>> 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.
>>
>>  
>>
> Thanks for the reminder on the security issue with eval.  In my case, 
> I am safe since I only intend to put globals in my eval.
>
>> Like I said, there's probably a better way to do whatever you are
>> trying to do....
>> _______________________________________________
>> New York PHP Talk Mailing List
>> AMP Technology
>> Supporting Apache, MySQL and PHP
>> http://lists.nyphp.org/mailman/listinfo/talk
>> http://www.nyphp.org
>>
>>
>>
>>  
>>
>
>
> _______________________________________________
> New York PHP Talk Mailing List
> AMP Technology
> Supporting Apache, MySQL and PHP
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.nyphp.org
>



More information about the talk mailing list