Global Objects in PHP

    global stars

    The message will talk about the fundamental in programming - global objects. I would say this is a scientific question that I would like to discuss. So, in order not to “shoot off your legs”, programmers do not program in the global area. Ok, everything is clear and extremely simple, dwell on this? Not in this stuff. As is well known, any action causes a chain of events and logical consequences.

    First, why create a dogma that you can not create? Instead, let's create a function stop_globals (), for example for the PHP language. The framework, at the beginning of the execution of its code, can execute it, and further attempts to work with the global area will cause PHP errors. Is this solution good?

    This is not all that could be discussed.

    The main reason for the existence of the aforementioned dogma is that it is possible to accidentally overwrite the values ​​of global variables, and this, in turn, can lead, including to difficult-to-localize errors in the program. And if you can use global variables for reading, but not for writing?

    Let's pay attention to the Universe around us. There are global objects in it: space, time, matter, energy, perhaps dark matter and energy. Similarly, based on my experience in web programming, there are a number of objects for which it is inconvenient to use Dependency Injection and such objects are inherently global. These are the database connection object, the `USER` object and others. To work with such objects in PHP, one could introduce the function super ('sky', 'user'), which would make $ sky and $ user variables superglobal, like $ _GET or $ _POST.

    Such a solution is not worse than the traditional dogma “it is impossible to program in the global domain”, since writing data to such variables will immediately be detected and PHP will generate an error, and the advantage will be as follows:

    1. Conceptually, global objects remain global, the program looks simpler.
    2. This approach is more productive. It is much faster to access a variable directly than to pre-pass its value through the stack. Means, processor stack which is used by compilers for transfer of parameters of functions. Anyway, the implementation of the DI pattern has overhead costs for the resources used.

    As you know, the PHP project positions itself as an elephant, because it provides many approaches in programming, a large number of functions and classes, an abundance of experimental code that is evaluated over time and can be developed or deleted in the future. In this regard, I ask free-thinking people to express their reasoned opinion on the questions:

    1. Do you support the introduction of the experimental functions super () and stop_globals ()?
    2. What do you think about the above idea as a whole?

    No holy war, thank you.

    In conclusion, I would like to mention my observation regarding laravel and the dangers of programming patterns. As it is known, laravel is called the “kladyazem of anti-patterns” . I consider this fact, as well as the free thinking of its author, allowed this project to become as popular as it is. Patterns are good for communicating programmers to point out some programmatic entity. But they also harm, confusing programmers, not allowing programs to be effective. Let's make programming easier.

    Also popular now: