Codeigniter 2 with variable reference error in version 5.6 of PHP

1

I'm 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

At line 257 has a strange code:

return $_config[0] =& $config;

This message began to appear since I updated the PHP version to 5.6.

Why does this error occur? How can I resolve it?

    
asked by anonymous 16.01.2017 / 13:00

1 answer

4

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 signature of the get_config() method, which forces the return to be a reference (variable).

If you can measure the impact of the change, another solution is to remove the & of the signature. Since this code snippet is part of ( Comum / Common ;)) of the framework, it can lead to unexpected behavior.

Signature:

function &get_config($replace = array())

Change:

return $_config[0] =& $config;

To: as corrected in the 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 in various php versions.

    
16.01.2017 / 13:09