Answer the question
In order to leave comments, you need to log in
Can global variables be used?
This article says that using global variables is not good.
1) Is it bad only in php or in all languages?
2) How to avoid their use? Carry every time
$pdo = connectDB($host,$user,$pass,$db,$charset);
вместо
$pdo = connectDB(); //а уже в самой connectDB(); использовать глобальные
Answer the question
In order to leave comments, you need to log in
1. About the article.
The author is hysterical and stupid.
The only claim he gave birth to is that the global variable can be rewritten. Fuck a disadvantage. And using while, you can go into an infinite loop. Following his logic, while cannot be used, it destroys the structure of the program. And more Moscow.
There are [at least] two ways to use global variables.
So, the second method is a guaranteed pass to ADB. This is a real way to bring to suicide a person who will then disassemble your program.
At the same time, there is nothing shameful in the first option. Everyone uses it, but for religious reasons, draping it into a static function, class, or container, but the essence of everyone will be exactly the same - to get access to the global service.
1. Taking into account item 2 from the previous section, it becomes clear that your second option is undesirable. Wherein.
2. I did not understand the phrase about "pull". Where and what are you going to carry? The connection code should be called EXACTLY ONCE, lie somewhere deep in the bootstrap, and not cause the slightest association with the word "drag".
And - yes. One hundred percent of the respondents will write to you not about your variables with a host and password, but about $pdo itself. Because the question about it is more logical and much more relevant: after all, you need the variables for the connection 1 time, and the connection itself - dozens of times in different places in the script.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question