There is no guideline in which say how we should design the system.
Therefore, we cannot say that it is right or wrong to store such data in the variable $GLOBALS
.
However, there is a concern regarding the consumption of unnecessary resources.
Practical example
$GLOBALS = array(
'foo1' = array(1, 2, 3, 4, 5),
'foo2' = array(1, 2, 3, 4, 5),
'foo3' = array(1, 2, 3, 4, 5),
'foo4' = array(1, 2, 3, 4, 5),
'foo5' = array(1, 2, 3, 4, 5),
'foo6' = array(1, 2, 3, 4, 5),
'foo7' = array(1, 2, 3, 4, 5),
'foo8' = array(1, 2, 3, 4, 5),
'foo9' = array(1, 2, 3, 4, 5),
'foo10' = array(1, 2, 3, 4, 5),
);
Suppose this array is much larger and that’s usually what happens.
Why port on all pages, all this data?
Imagine a page that invokes a function that only needs "foo10". No other data from this immense array will be used. However, they are all there occupying space in memory.
Something more economical is to load the data only when you really need it.
Example
foo10.php
<?php
return array(1, 2, 3, 4, 5);
php page.
<?php
$foo10 = include 'foo10.php';
This is sufficient, organized, simple and light.
It also allows the application to be written in a modular way. That is, it makes possible the re-usability of functions for diverse business logic applications.
Performance
Obviously, the cost of performance in low volume access systems is insignificant, but what matters here is that even in a small system, you have an application with organized codes. If it makes no difference in performance, then why not write in a more organized way?
However, the system does not need to reach titanic traffic volume like facebook, youtube, google, among others, to start presenting performance problems.
A small volume of 10 thousand unique accesses per day already makes micro-optimizations perceptible and, the subject we are talking about here has a much greater significance than a micro-optimization.
There is a performance cost in using include as well, which can be even more damaging than if the data were all in an array.
However, we can solve this by creating build cache structures. I’m not talking about using Memcache, APC, and the like. We can build intelligent cache structures using purely the standard native PHP features. Of course it takes a lot of work, but it pays in the end.
Build caches make an app over 800% fast. This means that the performance bottlenecks will be felt when the volume is at 50 or 60 thousand unique accesses per day and, this is a relatively high volume.
The vast majority of system developers do not have this kind of concern for various reasons, but in general it is because they find it unnecessary to worry about optimizations. In the first problem with performance go into despair or, for convenience appeal to loadbalance, cluster, mirrors. Finally, structures that significantly increase the cost of infrastructure and maintenance.
Abandoning the whole structure, switching to another technology is not something very smart unless it is something very outdated, unfeasible to continue using and, this is not the case of PHP.
PHP is as weak and poor as they say?
Always be suspicious when someone says that PHP is not good for high data volume and "this" or "that".
For many years, we PHP programmers have suffered from prejudice, being rated amateurs for using a weakly typed language that "allows total laziness" in application development. Actually the prejudice was quite strong until 2007 and has been decreasing dramatically since then.
This prejudice fell into myth when facebook publicly stated that it still uses PHP and Mysql. Although already exist at the time several other examples of high volume data systems running under PHP.
Instead of changing technology, they just perfected what they were already using.
So avoid believing the myth that big design has to be done with big, expensive and heavy technologies like JAVA and Oracle (just example). These days it’s so beaten that it sounds ridiculous.
Why PHP is the most criticized?
Simply because it is the most popular. It is estimated to be present in more than 85% of web systems worldwide.
There are also malicious criticisms of a commercial nature. A competitor wants to sell his fish and as he can not win on worthy merits, appeals to the low with slanders and spreading untruths.
Common sense
As always, common sense and discernment must prevail.
For example, why use a framework, however small and economical, to simply set up a 3-page static website and a 3-field contact form?
A simple $_POST[] and mail() already solves the job.
Finally, the techniques, tools and concepts should be applied as appropriate.
The final concept depends very much on the level of experience of those involved in the project. This is what differs a senior professional from the amateur in the technical field.
Have you ever done this? I don’t know if it’ll solve how you’re thinking. I need to leave but I’ll answer when I get back.
– Maniero
I’ve done the configuration part, but I don’t know if I leave it that way, because it’s a global variable.
– Laura
This gives a certain fear because it will load in the global variable all the settings and a lot of this data you end up not using all of them in a single page. Do you understand? I’d just be consuming memory. Your application gets heavy and this weighs in your pocket as well because you will need more powerful servers, which cost more. Think of something lean that saves resources. The more you can do with less resources, the better.
– Daniel Omine
The duration of a PHP page is so short that the allocation and displacement in blocks is even more efficient. In PHP, the more direct you go to the point, the greater the use of machine resources. Almost all academic theories are better in programming languages than in script ones, which is the case of PHP (which is not even a common loop, much less persistence of data structures in memory). In this case, you can rest assured, that global will not get in the way of anything. Also, internally, a class is allocated as global. Imagine her members as an array.
– Bacco
Thanks for the comment @Bacco.
– Laura