PHP - performance and memory issue with global variables

后端 未结 2 487
陌清茗
陌清茗 2020-12-18 14:58

Hypothetical situation: I\'m running a complex site in php, and i use a lot of global variables.

i could store the variables in an existing global scope, say $

相关标签:
2条回答
  • 2020-12-18 15:36

    Both are pretty bad. I would suggest using a singleton, or static classes.

    As for memory uses, there would be no noticable difference.

    0 讨论(0)
  • 2020-12-18 15:58

    Your global variables are already accessible in $GLOBALS['foo'], $GLOBALS['bar'] etc. This is a clearer indication inside function scope that they come from the global scope than using the global keyword. Should not affect performance in any meaningful way.

    Many will tell you that best practice is to avoid global variables in the first place and instead pass variables through function calls and object constructors.

    0 讨论(0)
提交回复
热议问题