Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Private Class Variables Permanently PHP

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

?>
like image 495
David Calvin Avatar asked Feb 12 '23 01:02

David Calvin


1 Answers

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.

like image 119
Jonathan Kuhn Avatar answered Feb 13 '23 21:02

Jonathan Kuhn