PHP: alternatives to a global variable?

I work on a PHP project and from time to time between the things that I read on the Internet and the things that I see on forums, etc. I keep reading that you should not use php globals. Making sure that I do not mix register_globals with PHP because I do not, I do research, but I really have not found why or any alternatives.

So my question is simple. Should I use the global in PHP? Also, if I shouldn't (or should), are there any alternatives? The reason is that I noticed that I need to access a variable defined in another file, and I need to reference or call this variable in a function, many functions, and I kind of get tired or use the code global $var_name; so much.

Any ideas (or am I just wrong)?

+7
source share
5 answers

Static classes and singletones are slightly better than global ones. Static classes simply group global variables, but the variables themselves are still available worldwide, separate instance variables. The same goes for Singletons. Although they use them, they should not be used as a general substitute for global variables. PHP makes it attractive, especially because of the need to declare global variables in functions, while static classes are available anytime, anywhere.

It is best to put the variables in the class (for example, "AppConfig" or a more specific class) and create an instance of this class to store certain values. Then pass this instance to all the methods in your structure. Thus, you do not rely on a specific implementation of Singleton and are truly flexible.

But, I must admit that this is a lot of work, especially when you have not experienced it yet. Thus, using a singleton is now probably good if you remember this answer when you feel that your singles are itching somewhere in the future.

+3
source

You are not mistaken, you just need to think about some kind of architecture for your application. :)

If you have shared data between classes, you should use a model containing that shared data and have an API available to all your classes to retrieve this variable.

For simplicity, you can use Singleton to store any shared data.

The PHP templates page contains a Singleton example. The idea of ​​Singleton is that you always get access to one instance (version) of this class, so if you change the variable there, it will be automatically changed elsewhere.

+2
source

Globals are bad if you write object oriented code. If you are procedural code, then global variables are just fine. If it uses objects, you need to use dependency injection ( new Object(new Collaborator) ). To handle this, you will probably eventually need to use a dependency injection container. If you start using static classes and singlets, you no longer write 100% OO code.

But the bottom line, if its normal procedural code, does not happen to global ones.

+2
source

As some others have said, there is nothing wrong with using globals; especially on a short script, it really can keep your code more readable than hiding it with an OO structure too large.

But you wrote: "I need [this variable] in a lot of functions, and I’m kind of tired or using the global $ var_name, so much code

This suggests what you really want for all of these functions, using $ var_name for the class. As a refactory of the first stage, you pass the variable from another file to the constructor and replace all of your $var_name with $this->var_name , and then split all the lines global $var_name; .

You can get one global instance of this class, but that's fine. Globals are not evil, but they need to be controlled and documented as your code becomes more complex.

If you don't already have one, Martin Fowler's refactoring book reads well to help you deal when your 100-line script is now 1000 lines long and knocks you down. (Examples are in java, but are still available to the PHP programmer.)

+2
source

In general, global variables introduce a code security and maintainability problem. If you use modern PHP, then a good solution is to have a static class that can act as a holder for all the global variables that you need. For example, a good example is sfConfig from the Symfony Framework. You can see the documents and code here for inspiration on how to make your own (or just clear the code that will be used in your own project .. it's pretty self-contained).

0
source

All Articles