PHP: Only variable references should be returned by reference

There is a Mobile Optimized version of this page (AMP). Open Mobile Version.

Pulling your hair out over this Notice message? Well here is an explanation for why you are getting this and how you can avoid it.

Take a look at the following code:

<?php
class myClass {
    public $var = 1;
 
    public function &getVar() {
        return($this->var);
        }
}
 
$obj = new myClass;
$value = &$obj->getVar();
?>

Now this code works completely fine, apart from every time it hits the “return” statement it chucks out a weird Notice error: Only variable references should be returned by reference.

But what does that actually mean? … Basically the return statement is returning the result of the variable and not the reference because of the () brackets around the variable.

so to return correctly by reference you would do this instead:

<?php
class myClass {
    public $var = 1;
 
    public function &getVar() {
        return $this->var;
        }
}
 
$obj = new myClass;
$value = &$obj->getVar();
?>

Kicking yourself now? Yes it is that easy…

Author: Dean Williams

I'm a Web Developer, Graphics Designer and Gamer, this is my personal site which provides PHP programming advice, hints and tips

0 0 vote
Article Rating
Subscribe
Notify of
guest
6 Comments
Inline Feedbacks
View all comments
Rolando

Greetings, I ran into the same problem, but was using the set, it had been running fine until the Notice were activated.

This was
"public function &setRutaUpdate($ RutaUpdate){
return $this->RutaUpdate = $RutaUpdate;
}"

I do not understand why the message, but I corrected basing your code and adding

"public function &setRutaUpdate($ RutaUpdate){
$this->RutaUpdate = $ RutaUpdate;
return $this->RutaUpdate;
}"

Well, I hope someone seeking this solution can help.

AndreaB.

hi! my error is: Notice: Only variable references should be returned by reference in /home/vn000150/public_html/admin/mailings/func/chron/htmlMimeMail.php on line 440 the code is: function &_addAlternativePart(&$obj) { $params['content_type'] = 'multipart/alternative'; if (is_object($obj)) { return $obj->addSubpart('', $params); } else { return new Mail_mimePart('', $params); } } error line: return new Mail_mimePart('', $params); do you know how fixed? I have the same error other 2 times: 1. the error is: Notice: Only variable references should be returned by reference in /home/vn000150/public_html/admin/mailings/func/chron/smtp.php on line 94 function &connect($params = array()){ if(!isset($this->status)){ $obj = new smtp($params); if($obj->connect()){ $obj->status = SMTP_STATUS_CONNECTED; } return $obj; }else{ $this->connection = fsockopen($this->host, $this->port,… Read more »

palash

This code is buggin me public function &execute_query($buffered = true, &$link)
{
$this->connection_recent = $link;

$this->querycount++;
if ($queryresult = $this->functions[($buffered ? "query" : "query_unbuffered")]($this->sql, $link))
{
$this->sql = "";
return $queryresult;
return $return;
}

$this->sql = "";
}

same error. but it's indicating the last ) , what should i do?

Adi Dobrescu

@palash

Try to return something from the else branch of the if statement. Eg.:
if(....)
{
....
return $queryresult;
}
......
$this->sql="";
return $null;
}