Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to setup global variables in oop?

Tags:

oop

php

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.

like image 719
Michael Joseph Aubry Avatar asked Sep 16 '13 03:09

Michael Joseph Aubry


1 Answers

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.

like image 171
Fluffeh Avatar answered Sep 21 '22 01:09

Fluffeh