开发者

The advantage / disadvantage between global variables and function parameters in PHP?

sorry i'm a beginner and i can't determine how good a question this is, maybe it sounds utterly obvious to some of you.

if our use of these two below is the same which is better?

function doSomething ($var1,$var2,..){
    ...
}

OR

function doSomething (){
    global $var1,$var2,..;
    ...
}

by our use I mean that I know that in the second scenario we can also alter the global variables' value. but what 开发者_StackOverflow中文版if we don't need to do that, which is the better way of writing this function? does passing variables take less memory than announcing global's in a function?


The memory usage is a paltry concern. It's much more important that the code be easy to follow and not have... unpredicted... results. Adding global variables is a VERY BAD IDEA from this standpoint, IMO.

If you're concerned about memory usage, the thing to do is

function doSomething (&$var1, &$var2,..) {
   ...
}

This will pass the variables by reference and not create new copies of them in memory. If you modify them during the execution of the function, those modifications will be reflected when execution returns to the caller.

However, please note that it's very unusual for even this to be necessary for memory reasons. The usual reason to use by-reference is for the reason I listed above (modifying them for the caller). The way to go is almost always the simple

function doSomething ($var1, $var2) {
    ...
}


Avoid using global variables, use the passing variables in parameters approach instead. Depending on the size of your program, the performance may be negligible.

But if you are concerned with performance here are some key things to note about global variable performance with regards to local variables (variables defined within functions.)

  • Incrementing a global variable is 2 times slow than a local var.
  • Just declaring a global variable without using it in a function also slows things down (by about the same amount as incrementing a local var). PHP probably does a check to see if the global exists.

Also, global variables increase the risk of using wrong values, if they were altered elsewhere inside your code.


Write it to take parameters. Maintainability is far more important than micro-optimization. When you take parameters, the variables can not be modified in unexpected places.


Although it is not good practice as long as you guarantee that the global is never written, but only read you will have the flexibility of paramaters.

As as alternative, you can pass one parameter (or two if it really goes with the function, like exp) and the rest in an array of option (a bit like jquery does). This way you are not using globals, have some parameter flexibility and have clearly defined the defaults for each parameter.

function get_things($thing_name,$opt= array() {
   if(!isset($opt["order"])) $opt["order"]= 'ASC';
}


Pass in parameters, avoid globals. Keeping only the scope you need for a given situation is a measure of good code design. You may want to look at PHP variable scope...

http://php.net/manual/en/language.variables.scope.php

An excellent resource, with some pointers on what is best practices and memory management.


As of PHP 4 using global with big variables affects performance significantly.

Having in $data a 3Mb string with binary map data and running 10k tests if the bit is 0 or 1 for different global usage gives the following time results:

function getBit($pos) {
global $data;
$posByte = floor($pos/8); 
...
}

t5 bit open: 0.05495s, seek: 5.04544s, all: 5.10039s

function getBit($data) {
 global $_bin_point;
 $pos = $_bin_point; 
 $posByte = floor($pos/8); 
}

t5 bit open: 0.03947s, seek: 0.12345s, all: 0.16292s

function getBit($data, $pos) {
$posByte = floor($pos/8); 
...
}

t5 bit open: 0.05179s, seek: 0.08856s, all: 0.14035s

So, passing parameters is way faster than using global on variables >= 3Mb. Haven't tested with passing a $&data reference and haven't tested with PHP5.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜