PHP: Only variable references should be returned by reference

Take the following code example:


class myClass {

public $var = 1;

public function &getVar() {




$obj = new myClass;

$value = &$obj->getVar();


This looks like valid code, right? PHP wants you to use the & in both places to indicate that you want to return by reference, and that’s what we have. But how come that this code still generates the notice?

Quote from php “return” manual (

Note: You should never use parentheses around your return variable when returning by reference, as this will not work. You can only return variables by reference, not the result of a statement. If you use return ($a); then you’re not returning a variable, but the result of the expression($a) (which is, of course, the value of $a).

While it’s a very simple mistake, it did take me while to figure this out.

Leave a comment