0

I reduced my globals to only one. Seems a bit weird to inject a class with only one variable and a getter function, but I don't want any implicit dependencies, I want them all explicit and documented. Also I only want the "globals" accessible to the classes I give access to. So in a sense they are not global. Need to re-name to shared. LOAD_ON is the only variable that I need in multiple classes.

Is this the correct way (best practice) to implement a "global" variable when trying to adhere to SOLID / DRY (Don't Repeat Yourself) / OOP ( Object Oriented Programming).

<?php 

class GlobalClass
{
    private $LOAD_ON = 0;
    public function getLoad()
    {
        return $this->LOAD_ON;
    }
}
  • do you use something like the registry design pattern to workaround using $GLOBALS? – Hajo Apr 19 '12 at 19:01
  • The answer here points that this is in the correct direction -> http://stackoverflow.com/questions/1151341/registry-design-pattern-good-or-bad –  Apr 19 '12 at 19:09

1 Answers1

1

If you got only 1 'global' and don't need to change it, constants are the best practice.

define('LOAD_ON', 0);

Use it in this way

if(LOAD_ON === 0){
 ///...
LHolleman
  • 2,486
  • 2
  • 18
  • 19
  • Yes, and so is your class. I could just use $gc = new GlobalClass(); – LHolleman Apr 19 '12 at 19:10
  • Registry pattern provides encapsulation...so now it will be new Shared();...where Shared implement this pattern....0 Globals...may seem over-engineered...but for consistency I'm going to use it. –  May 23 '12 at 15:31