I have a variable that is used in so many of the functions of my web site that it would be much easier if all of them had access to it rather than me having to pass it every time. Is that possible and, if so, how is it done?

    I'm with the camp that is anti-globals. 🙂 This could be a case where some object-oriented programming might be called for. You could create a class that has related functions (methods) grouped within it, and which make use of a class variable which would store this "global" value you want them all to use.

    <?php
    class Example
    {
       private $myVar;
       public function __construct($var)
       {
          $this->myVar = $var;
       }
       public function foo()
       {
          return "Foo " . $this->myVar;
       }
       public function bar()
       {
          return "Bar " . $this->myVar;
       }
       public function fubar()
       {
          return "Fubar " . $this->myVar;
       }
       public function set($value)
       {
          $this->myVar = $value;
       }
    }
    
    $test = new Example("Value 1");
    echo $test->foo();
    echo "<br />\n";
    $test->set("New value");
    echo $test->fubar();
    
      7 days later

      Also think about using as constants if it is not gonna change

        Write a Reply...