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