0

I want to have access to a global variable without having to redeclare every function. Here is an example.

$mobile = 1;


class Database
{


    private $connect;


    function one($argumentarray)
    {
     global $mobile;
     echo $mobile;
    }

    function two($argumentarray)
    {
     global $mobile;
     echo $mobile;
    }

    function three($argumentarray)
    {
     global $mobile;
     echo $mobile;
    }
}

I have about 100 functions and i need access to the $mobile variable in all of them. Is there a way to do this without having to do global $mobile in every function?

rdonatoiop
  • 1,185
  • 1
  • 14
  • 28
user892134
  • 3,078
  • 16
  • 62
  • 128

6 Answers6

2

The best way would be to NOT use the global. Design your program in a way that it can live without globals. That's it.

A simple design that comes in mind, might look like:

class Configuration {

    protected static $mobile = 1;

    // let's say it's write protected -> only a getter. 
    public static mobile() {
        return self::$mobile;
    }
}


class Database {

    // use the config value
    function one() {
        if(Configuration::mobile()) {
            ...
        }
    }
}
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
1

Use the $GLOBALS array.

$GLOBALS["mobile"]

Or you could store the variable in your class - which is cleaner in my opinion.

ManiacTwister
  • 311
  • 1
  • 12
  • While this requires editing every single function, it should be a 30 second job with any decent editor. I'd recommend this until you have the chance of doing a proper redesign. – Álvaro González Jan 28 '14 at 15:56
1

Well, You could do something like this:

class Database
{

    private $connect;
    private $mobile;

    function __construct() {
        #assign a value to $mobile;
        $this->mobile = "value";
    }

    function one($argumentarray)
    {
     echo $this->mobile;
    }

    function two($argumentarray)
    {
     echo $this->mobile;
    }

    function three($argumentarray)
    {
     echo $this->mobile;
    }
}
Ricbermo
  • 815
  • 1
  • 6
  • 28
1

Already 5 answers and nobody mentioned static class variables yet.

Copying the variable in the constructor would be wasteful, impractical and hurt readability. What if you need to change the value of this global later on? Every object will be stuck with an obsolete copied value. This is silly.

Since you need a prefix to access this "global" anyway, let it be self:: or Database:: rather than this-> :).

It will

  • allow you to set the variable directly from the class definition,
  • not waste memory doing a copy of it for each object
  • make it accessable from outside if you whish so (if you declare it public)

In your example:

 class Database {
    static private $mobile = "whatever";

    function one($argumentarray)
    {
        echo self::$mobile;
    }

    function two($argumentarray)
    {
        echo self::$mobile;
    }

    function three($argumentarray)
    {
        echo self::$mobile;
    }
}

If you want to allow modifications from outside :

static private $mobile;
static function set_mobile ($val) { self::$mobile = $val; }
static function get_mobile ($val) { return self::$mobile; }

or

static public $mobile;
kuroi neko
  • 8,479
  • 1
  • 19
  • 43
  • @hek2mgl I just copied the example given by the OP – kuroi neko Jan 28 '14 at 16:12
  • But however, a `static` variable that belongs to `Database` makes no sense for me (I expect that the global is used in other parts of the code as well).. I would suggest something like `class Config { protected static $mobile; }` + a getter Then you can access `Config::mobile()` from everywhere. – hek2mgl Jan 28 '14 at 16:17
  • @hek2mgl Frankly I think the OP's question makes little sense, but the answers suggesting to duplicate a global into every single instance of a class look plain silly to me. – kuroi neko Jan 28 '14 at 16:21
  • For some simple usage a static like in my example is good. In a more elaborated application, there would be a well defined way to access config values, then static variables aren't needed any more, or directly spoken they lead the the same problematic design issues as globals. However, in the context of *this* question, your idea isn't bad.. That's why -> +1 ;) – hek2mgl Jan 28 '14 at 16:25
0

Maybe a class attribute, and set it with the constructor?

class myClass {
    private $mobile;
    /* ... */
}

In the constructor, do:

public function __construct() {
    $this->mobile = $GLOBALS['mobile'];
}

And then use:

$this->mobile

from everywhere!

Ashraf Samhouri
  • 408
  • 3
  • 8
  • 1
    This would not track modifications on that variable. You need a reference – hek2mgl Jan 28 '14 at 15:57
  • Why not inject the reference to the variable via the constructor? Or, make the class member public, and modify the class instance when needed. – crush Jan 28 '14 at 15:58
  • Best is not to use `globals`. I've never seen that a global made a better design – hek2mgl Jan 28 '14 at 15:59
  • Yeah it's better not to use globals, that's for sure! And, yes we can refer the variable or just use it from everywhere, or make it static (lots of ways to do it). However, I second hek2mgl's view. No need for globals, usually. – Ashraf Samhouri Jan 28 '14 at 16:05
0

Pass the variable to the constructor, and set a class level variable. It can then be accessed in the varius methods using $this->

$mobile = 1;


class Database
{


    private $connect;
    private $mobile

    function __construct($mobile){
        $this->mobile=$mobile;
    }

    function one($argumentarray)
    {

     echo $this->mobile;
    }

    function two($argumentarray)
    {

     echo $this->mobile;
    }

    function three($argumentarray)
    {

     echo $this->mobile;
    }
}

$db = new Database($mobile);
Steve
  • 20,703
  • 5
  • 41
  • 67