4

The Problem: I would like to either permanently set private variables in a class, and then access them with a getter function from outside the class. The issue is every time I instantiate a new the class and create an object it destroys the previously set variables. In the example provided, I do not want to pass the object via the calling function "getAgain". I'd like to simply access the globalVars class without destroying any of the set variables. I understand that by creating a 'new Object' in essence destroys current not static vars. SO:

  • How do you permanently set private variables within a class?
  • OR
  • How do you call a function (getter/setter) without re-instantiating the class (as to not destroy the currently set var(s)).

I fear I am not approach this the right way or maybe my methodology is flawed.

<?php

class globalVars{

   private $final = "Default Foo </br>";

   public function setter($param){
      $this->final = $param;
   }

   public function getter(){
      return $this->final;
   }

}

class driver{

   function __construct($param){
        $globalVars = new globalVars();
        $globalVars->setter($param);

        $val = $globalVars->getter();
        echo $val;

        $this->getAgain();
   }

   function getAgain(){
       $globalVars = new globalVars();
       $val = $globalVars->getter();
       echo $val;
   }
}


$input = "Change to Bar </br>";

$driver = new driver($input);

?>
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
David Calvin
  • 194
  • 1
  • 11

2 Answers2

6

First off, the variables you set don't get destroyed when you re-instantiate the class. This is something called encapsulation meaning that every instance of a class has it's own set of properties which is critical to OOP. It is more like the values are copied from the base class definition on each instantiation which means you get the default values. If you want a variable to be available across all instances of a class, that is what a static variable is for.

<?php
class globalVars{

    private static $final = "Default Foo </br>";

    private $instance = "Some default value";

    public function setStatic($param){
        self::$final = $param;
    }

    public function setInstance($param){
        $this->instance = $param;
    }

    public function getStatic(){
        return self::$final;
    }

    public function getInstance(){
        return $this->instance;
    }
}

$test = new globalVars();
$test->setStatic("Foo");
$test->setInstance("Bar");

$test2 = new globalVars();
$final = $test2->getStatic();
$instance = $test2->getInstance();

echo $final;
//outputs "Foo"

echo $instance;
//outputs the default value for the instance property because in this
//instance the value was never changed.

echo $test->getInstance();
//outputs "Bar" because the instance property was changed in this instance.

Edit: I changed the class a little to show the difference between a static and instance property.

Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43
2

You can simply use static variable for this.

Or if you want to access one class globally, you can use singleton pattern, for that add a static variable for example $_obj and a static getter. Now every time you need that class just call globalVars::getObj() and you have it.

<?php

class globalVars{
   private static $_obj;
   public static getObj()
   {
      if(!isset(self::$_obj)) self::$_obj = new globalVars();
      return self::$_obj;
   }

   private $final = "Default Foo </br>";

   public function setter($param){
      $this->final = $param;
   }

   public function getter(){
      return $this->final;
   }

}

class driver{

   function __construct($param){
        $globalVars = globalVars::getObj();
        $globalVars->setter($param);

        $val = $globalVars->getter();
        echo $val;

        $this->getAgain();
   }

   function getAgain(){
       $globalVars = globalVars::getObj();
       $val = $globalVars->getter();
       echo $val;
   }
}


$input = "Change to Bar </br>";

$driver = new driver($input);
halfer
  • 19,824
  • 17
  • 99
  • 186
Ali Akbar Azizi
  • 3,272
  • 3
  • 25
  • 44
  • 1
    So you are essentially creating a singleton pattern? It could be explained a little better about what is happening. Also, wouldn't `getObj` need to be public? – Jonathan Kuhn Jan 28 '15 at 22:10