Michael Joseph Aubry
Michael Joseph Aubry

Reputation: 13492

Best way to setup global variables in oop?

I am having a hard time finding an answer to this, I am sure it's there in front of me but I cant put it together.

    //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 so my code feels more organized, I feel I can read my code better when paths are defined this way and it makes it easier to change the variables so that it applys throughout the whole code if needed.

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

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

The primary question here, I am wondering if this is bad practice, if not is there a better way then having to define global $GLOBAL inside each method. If however it is bad practice can you show me good practice?

There is a one other thing I am really curious about. Inside the main __construct you see I don't use global $GLOBAL because it works without it, but inside that require_once file is another class which has methods that must use global $GLOBAL inside of them. Hope someone can explain that.

Some are saying this is bad practice, I think. I read there is a singleton pattern(bad practice) and a global pattern. Not sure what I did above constitutes, I am just a little lost from what I should do here with what I am trying to achieve.

Upvotes: 6

Views: 7136

Answers (4)

Fluffeh
Fluffeh

Reputation: 33542

An object really should have everything needed for it to perform any function it is designed to. If not, the data should be passed via a param to it.

Part of the whole point of objects versus procedural code is that objects can be re-used over and over and in many places.

Lets use this example of why using Globals and OOP is a bad idea:

Lets say you have a great front-end bit of code, and you write it all through objects. Sometime down the track, you need to create additional reporting on the data in your site. You start to code and realize that you can re-use your classes from the front to achieve almost everything you need - but alas your code contains numerous references to globals that are only visible in the front end if called from a certain script.

That's basically just made your objects rather un-reusable.

Although it's probably not the best practise, but I often write a quick class that grabs a hold of various $GET, $_POST and other variables which might be considered "Globals" such as URL params and the like, then in my code, I either pass this information directly to the objects/functions as needed or indeed pass the entire object (rarely).

Using this approach, I am always perfectly ready to re-use objects knowing EXACTLY what they need to function as they are all required in params.

Upvotes: 9

Ja͢ck
Ja͢ck

Reputation: 173662

From your code it seems that you're just looking for a way to resolve your class names into file names. For that purpose it's better to use auto loaders. It takes the formality of how your classes map to file names away from the bulk of your code so that you can concentrate on actual development.

Organizing your plugins can be done by using namespaces.

Second, in most cases you don't want to instantiate classes inside the constructor because it leads to coupling, and too much coupling can stifle your project later; instead, inject the dependency:

class My_Class 
{
    private $settings;

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

To call it:

$c = new My_Class(new My_Settings());

Upvotes: 3

Samuel  Arroyo
Samuel Arroyo

Reputation: 19

Use this example to solve your problem

The global keyword doesn't work the way you think it does. It does not make a variable have global scope. All it does is specify to the function that you call it in that you want to use the variable in the outer scope.

For example:

$a="test";

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

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

Upvotes: -3

invisal
invisal

Reputation: 11181

Why don't you use $GLOBALS? You don't even need to use global keyword.

The primary question here, I am wondering if this is bad practice

Well, I believe most people would say having a global state is bad practice because it is harder to manage or doing unit test. Most people would suggest you to use "dependency injection" which instead of depend on global state, you need to inject what the class need directly.

Upvotes: 4

Related Questions