0

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.

3
  • You mean you want PHP to predict what you're going to change an object's properties to before you actually change them? Commented Jun 12, 2014 at 9:23
  • No, I'm after setting a reference to the variable, so that when the variable is updated outside of the class, it updates the class variable too Commented Jun 12, 2014 at 9:24
  • 2
    You want to take a look at References in PHP: php.net/manual/en/language.references.php Commented Jun 12, 2014 at 9:26

3 Answers 3

3

Perhaps using by reference

class Test {

    private $var = '';

    function setVar(&$input) {
        $this->var = &$input;
    }

    function getVar() {
        echo 'Var = ' . $this->var . '<br />';
    }

}


$string = null;
$test = new Test();
$test->setVar($string);

$string = 'Hello';

$test->getVar();

$string = 'Goodbye';

$test->getVar();
Sign up to request clarification or add additional context in comments.

1 Comment

Ah yes, that's it! I was only adding the & to the method parameter instead of the parameter AND the return value! Thank you
2

Make the setVar() function pass the arguments by reference:

function setVar(&$input) {
    $this->var = &$input; // Note the & before $input
}

Working demo

2 Comments

Ah yes, that's it! I was only adding the & to the method parameter instead of the parameter AND the return value! Thank you. I've accepted Mark Baker's answer as he beat you by sheers seconds, but I thank you just the same :)
@JamieBicknell: No worries. I was preparing the demo - that slowed me down. :)
-2

Change $var to public and you can do this

<?php

class Test {

    public $var;

    function getVar() {
        echo 'Var = ' . $this->var . '<br />';
    }

}

$test = new Test();
$test->var = 'Hey dude';
echo $test->var;

Outputs

Hey Dude

2 Comments

Hi Dan, whilst this is a possibility, I wanted to see if I can do it without doing that way, but thank you for your input :)
Yeah, I actually missed the last third of your question and just re-read it. :-/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.