PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I inherited some php code. The program uses global variables to pass around arrays of information. It work, but it seems to not be in the spirit of OO programming. Here is example that illustrates my point. Can someone propose a better way to access variables in class functions, outside the scope of the function?
Generally, the whole idea of global variables is bad. For instance, it's hard to keep track of where in the code global variables are modified since the entire application has access to them.
The best thing would be to redesign the classes so that they do not use global variables. Pass the required variables to the function either as a parameter to the function itself or as a parameter in the constructor.
However, if this redesign takes a lot of time, you might want to consider keeping the global variables in order to save time. A possible middle way could be to put all the global variables in a class, in order to have a common "gateway" through which all global variable access is controlled. Make this class itself global, and required modification in existing code should be some rather simple replacing.