Best way to set global variables in oop?

It is difficult for me to find the answer to this question, I am sure that it is in front of me, but I can not collect it.

//Global Variables global $GLOBAL; $GLOBAL['myPluginPath'] = dirname( __FILE__ ); //Main Object class My_Class { public function __construct() { // Initialize Settings require_once $GLOBAL['myPluginPath'] . '/settings.php'; $My_Settings = new My_Settings(); } } 

I want to use a variable to make my code feel more organized, I feel that I read my code better when paths are defined this way, and this makes it easy to change the variables so that they apply throughout the code if necessary.

I can make the variables work by writing this inside my methods.

  public function WhatEverFunc() { global $GLOBAL require_once $GLOBAL['myPluginPath'] . '/settings.php'; } 

The main question here, I wonder if this is bad practice, if there is no better way, then define global $GLOBAL inside each method. If, however, this is bad practice, can you show me good practice?

There is one more thing that I am very interested in. Inside the main __construct you see that I am not using global $GLOBAL because it works without it, but inside that require_once is another class that has methods that should use global $GLOBAL inside them. Hope someone can explain this.

Some say this is bad practice, I think. I read that there is a singleton pattern (bad practice) and a global pattern. Not sure what I did above, I just lost a little what I have to do here with what I'm trying to achieve.

+6
source share
4 answers

Why don't you use $ GLOBALS ? You do not even need to use the global .

The main question here, I wonder if this is bad practice.

Well, I believe that most people will say that global state is bad practice because it is more difficult to manage or do unit test. Most people will suggest you use a dependency injection, which, instead of being dependent on the global state, you need to enter what the class needs directly.

+4
source

The object really should have everything necessary to perform any function for which it is intended. If not, data should be passed using the parameter.

Part of the whole point of objects and procedural code is that objects can be reused many times and in many places.

Let's use this example, why using Globals and OOP is a bad idea:

Suppose you have excellent external code and you write it through objects. Sometime down the track, you need to create additional reporting on the data on your website. You begin to code and realize that you can reuse your classes in front to achieve almost everything you need, but, alas, your code contains numerous references to global variables that are visible only in the interface if called from a specific script.

This basically just caused your objects to not be reused.

Although this is probably not the best practice, I often write a quick class that captures various $GET , $_POST and other variables that can be considered "global", such as URL parameters, etc., then in my code I either pass this information directly to objects / functions as needed, or I really pass the whole object (rarely).

Using this approach, I am always ready to reuse objects, knowing EXACTLY what they need to perform, since they are all needed in the parameters.

+9
source

From your code, it seems like you're just looking for a way to resolve class names into file names. To do this, it is better to use autoloaders . Formality is required for how your classes map file names to the main body of your code so that you can focus on the actual development.

Organizing your plugins can be done using namespaces .

Secondly, in most cases, you do not want to instantiate classes inside the constructor, because this leads to a join, and too many joins can drown your project later; insert a dependency instead:

 class My_Class { private $settings; public function __construct(My_Settings $settings) { $this->settings = $settings; } } 

To call it:

 $c = new My_Class(new My_Settings()); 
+3
source

Use this example to solve your problem.

The global keyword does not work the way you think. It does not make a variable a global scope. All he does is to indicate the function you are calling in that you want to use the variable in the outer scope.

For instance:

 $a="test"; function something() { global $a; echo $a; //Outputs: test } 

If you want to make the global variable so that it can be accessed from the class, you need to use the $ GLOBALS super galaxy.

-3
source

Source: https://habr.com/ru/post/953887/


All Articles