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…






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.
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 »
any returns where there is logic, for instance:
return $this->auth ? $this->ehlo() : $this->helo();
Try changing it so it returns a variable you setup:
$return = $this->auth ? $this->ehlo() : $this->helo();
return $return;
This should stem at least some of your problems with the warnings, however I'm finding it difficult to read your comment and compare the code to the warning's you have included.
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?
It appears you dont have the PHP version required, it is valid in PHP 4.0.4 and up to 5.4.0 where they removed it, try changing &$link to just $link. Should work fine!
@palash
Try to return something from the else branch of the if statement. Eg.:
if(....)
{
....
return $queryresult;
}
......
$this->sql="";
return $null;
}