Codeigniter 2 with variable reference error in PHP version 5.6
I am using Codeigniter version 2.1.3 and when I open any page the following error is displayed:
A PHP Error was encountered
Severity: Notice
Message: only variable references should be returned by reference
Filename: core / Common.php
Line Number: 257
In line 257
has a strange code:
return $_config[0] =& $config;
This message started to appear since I updated the version from PHP to 5.6.
Why does this error occur? How can I solve it?
1 answers
The Codeigniter framework has a great legacy, so it uses some techniques that have fallen into disuse. The problem in this case is the &
in the method signature get_config()
, which forces the return to be a reference (variable).
If you can measure the impact of the change, another solution is to remove &
from the signature. How this snippet of code is part (Comum
/ Common
;) ) of the framework, it may result in unexpected.
Signature:
function &get_config($replace = array())
Change:
return $_config[0] =& $config;
To: as fixed in version 2.2.x
$_config[0] =& $config;
return $_config[0];
A simple way to reproduce the error is with the following code:
function &olaMundo(){
return 'Ola mundo';
}
echo olaMundo(); //Notice: Only variable references should be returned by reference in
Correction:
function &olaMundo(){
$x = 'Ola mundo';
return $x;
}
Functional example run on various php versions.