user656925
user656925

Reputation:

Only One "Global" variable - Implementing Correctly?

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;
    }
}

Upvotes: 0

Views: 100

Answers (1)

LHolleman
LHolleman

Reputation: 2596

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){
 ///...

Upvotes: 1

Related Questions