NYCPHP Meetup

NYPHP.org

[nycphp-talk] Dynamically write functions

Ken Robinson kenrbnsn at rbnsn.com
Fri May 13 05:08:38 EDT 2005


At 06:39 PM 5/12/2005, 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.

You might want to re-think how you handle the globals in the first place.

Since all global variables are stored in the $_GLOBALS super array, 
why don't you either reference the global variables as an index of 
that array ($_GLOBALS['variable_name']) or use the extract() function 
at the start of each function to get variable names?

Ken 




More information about the talk mailing list