Apologies for the wording on this question, I'm having difficulties explaining what I'm after, but hopefully it makes sense.
Let's say I have a class, and I wish to pass a variable through one of it's methods, then I have another method which outputs this variable. That's all fine, but what I'm after is that if I update the variable which was originally passed, and do this outside the class methods, it should be reflected in the class.
I've created a very basic example:
class Test {
private $var = '';
function setVar($input) {
$this->var = $input;
}
function getVar() {
echo 'Var = ' . $this->var . '<br />';
}
}
If I run
$test = new Test();
$string = 'Howdy';
$test->setVar($string);
$test->getVar();
I get
Var = Howdy
However, this is the flow I would like:
$test = new Test();
$test->setVar($string);
$string = 'Hello';
$test->getVar();
$string = 'Goodbye';
$test->getVar();
Expected output to be
Var = Hello
Var = Goodbye
I don't know what the correct naming of this would be, and I've tried using references to the original variable but no luck.
I've come across this in the past, with the PDO prepared statements, see Example #2
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (?, ?)");
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $value);
// insert one row
$name = 'one';
$value = 1;
$stmt->execute();
// insert another row with different values
$name = 'two';
$value = 2;
$stmt->execute();
I know I can change the variable to public and do the following, but it isn't quite the same as how the PDO class handles it, and I'm really looking to mimic that behaviour.
$test = new Test();
$test->setVar($string);
$test->var = 'Hello';
$test->getVar();
$test->var = 'Goodbye';
$test->getVar();
Any help, ideas, pointers, or advice would be greatly appreciated, thanks.