Re: using global to pass vars to functions
| From: | Chris Lee | Date: | Fri, 01 Dec 2000 07:32:54 +0000 |
| Subject: | Re: using global to pass vars to functions | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-28304@lists.php.net to get a copy of this message | ||
Sticking to good programming practice I really really try and minimize my
ise of globals. you should never assign a global variable in a function.
Its hard for others to see whats going on.
you execute a function and it changes a variable that wasn't passed or
returned? thats gona get confusing.
I allways try and make my functions do one thing (this makes then very
portable), if they are going todo more then one thing they return the
value as an array, or I try and use classes.
I should make one note too, in classes if the function is doing something
I try and allways make the function return the same value as the variable
its modifing.
There are good reasons for global variables and I still use them, but I
tend to only use them for constants.
I wish I could referense some good *programming style* sites, but I dont
have any such.
Chris Lee
Mediawaveonline.com
In article <v03007801b64da9adc6a1@[195.2.137.75]>, andy@kinonet.com
wrote:
> I am working on a moderately large website, running from a mySQL
> database. As the site will be added to, upgraded and redesigned once it
> is up, I want to separate the HTML and the PHP code as much as possible
> and to keep the PHP functions, etc. left embedded in the pages as simple
> as possible.
>
> I have moved all of the functions to shared include files. In general,
> each web page calls a function to retreive data from a mySQL database
> and puts this information into variables; later in the page, the
> contents of these variables are displayed via echo statements.
>
> I know that it is a little unconventional, but is there any problems or
> drawbacks (regarding performance, etc.) in using global to get values
> into and out of the function, rather than sending and receiving them as
> parameters? Or is it better to keep the number of global variables used
> to a minimum?
>
> What I mean is using a function like this:
>
> function find_things () {
> global $var_in1, $var_in2, $var_out1, $var_out2;
> ... perform mysql query ...
> }
>
> and calling it from the web page like this:
>
> <?php find_things (); ?>
>
>
> Rather than having a function like this:
>
> function find_things ($var_in1, $var_in2) {
> ... perform mysql query ...
> return array ($var_out1, $var_out2);
> }
>
> and calling it like this:
>
> <?php list ($var_out1, $var_out2) = find_things ($var_in1, $var_in2); ?>
>
>
> It seems to me that this would make the function calling code much
> simpler, and less likely to be altered accidentally when the pages are
> redesigned. It would also mean that I don't have to change every line
> that calls the function (in every page) when the function is rewritten
> or developed and, for examle, changes the number or order of parameters
> that it takes. By using globals, the only file that gets changed is the
> PHP include with the function in.
>
> The system for naming variables is consistent throughout the whole site.
>
>
> Thanks
>
> Andy Clarke
>
>
> -----------------------------
> Andy Clarke
> 78 West Kensington Court
> Edith Villas London W14 9AB
>
> Phone: 44 (0)20 7602 3382 Mobile: 07947 418177 Email:
> andy@kinonet.com
> -----------------------------
>
>
>
>