Forum Moderators: coopster

Message Too Old, No Replies

static variables versus $GLOBALS array

         

Mister_L

11:57 pm on Sep 9, 2011 (gmt 0)

10+ Year Member



When you're working with MVC design pattern, and you want to make variables accessible in the global scope, some say it is better to do it with static methods/properties than with $GLOBALS array.Why is that?

Thanks.

Readie

4:11 pm on Sep 10, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, static variables are shared between all instances of that class, so in a sense they are global. You can get/set them at any time outside of the class with a className::$varName too (as long as they are public). I suppose the best reason I can think of to use a static var over GLOBALS is it reduces the chances of clashing variable names when different scripts are ran on the same page load.