Where to store global variables?

0 like 0 dislike
65 views
Where the terms competent architecture to store global variables?


For the sake of clarity of the issue, describe the other conditions of the problem, although perhaps a default would be implied. Other conditions are:

* OOP;

* first and foremost, interested in the specifics of the web application built according to MVC architecture.


Example of PHP code:

class Registry { private static $_data = array(); /** * To set a variable * @return true/false */ public static function set($key, $value) { if(array_key_exists($key,self::$_data)){ return false; } self::$_data[$key] = $value; return true; } /** * Get variable * @return value/null */ public static function get($key) { if( !array_key_exists($key, self::$_data) ){ return null; } return self::$_data[$key]; } /** * Delete a variable */ public static function delete($key) { unset(self::$_data[$key]); } }



Whether this option is good?

Maybe there is a way easier and more graceful?

It may be possible to do without global variables?

Please share the experience.
by | 65 views

6 Answers

0 like 0 dislike
"It may be possible to do without global variables?" — this question you have to ask yourself personally.
\r
And so, you have a main model that stores some properties. In turn, each vyuha and the main controller have a reference to the model and thus have access to the desired property of the model.
by
0 like 0 dislike
My inner voice tells you that the presence of global variables means insufficiently thought-out application architecture. I.e., each variable has its own meaning and must belong to the class that owns the meaning of this variable.
by
0 like 0 dislike
And why actually you need global variables?
by
0 like 0 dislike
Global variables are not needed. If you need copies of objects — use the Singleton pattern. And in all other cases, it is most likely just a wrong architecture.
by
0 like 0 dislike
Dependency injection ?!
by
0 like 0 dislike
Can You give an example of when You really need a global, inside the application variables?
by

Related questions

0 like 0 dislike
5 answers
0 like 0 dislike
3 answers
0 like 0 dislike
4 answers
asked Jun 11, 2019 by Barney_Gubmle
0 like 0 dislike
1 answer
0 like 0 dislike
3 answers
asked Apr 28, 2019 by tmgrom
110,608 questions
257,186 answers
0 comments
28,881 users