简体   繁体   中英

CodeIgniter problems after upgrading to PHP 7.3

I have upgraded my PHP to version 7.3.4 for my CodeIgniter 3.1.10 installation. After this I noticed that some parts of my website are not working anymore as before (with PHP 5.6) they did.

One of the use cases is when I type in a faulty URL like so:

www.abc.com/nl-be/antwerpen/this-is-a-bad-url

Normally this would go to my 404 page , but right now I first got the following error:

Message: Call to a member function helper() on null

Filename: D:\wamp\www\codeigniter\application\hooks\LanguageLoader.php

The code is:

class LanguageLoader
{
    function initialize() {
        $ci =& get_instance();
        $ci->load->helper('language');

        $site_lang = $ci->session->userdata('site_lang');
        if ($site_lang) {
            $ci->lang->load('message',$ci->session->userdata('site_lang'));
        } else {
            $ci->lang->load('message',$ci->session->userdata('site_lang'));
        }
    }
}

I managed to find a work-around to get past this problem using the code below (Is this correct?):

class LanguageLoader
{
    function initialize() {
        $CI =& get_instance();
        if ($CI === null) {
            new CI_Controller();
            $CI =& get_instance();
        }
        $CI->load->helper('language');

        $site_lang = $CI->session->userdata('site_lang');
        if ($site_lang) {
            $CI->lang->load('message',$CI->session->userdata('site_lang'));
        } else {
            $CI->lang->load('message',$CI->session->userdata('site_lang'));
        }
    }
}

After checking the value of $CI, this time it would not be null, but would be filled in. At this point after going to the same faulty URL, i am getting the following error message:

Message: call_user_func_array() expects parameter 1 to be a valid callback, class 'Error' does not have a method 'my404'

Filename: core/CodeIgniter.php

The code in CodeIgniter.php at that line contains:

call_user_func_array(array(&$CI, $method), $params);

The error is especially weird, since i have a my404 method in my Error class:

class Error extends My_Controller {
    public function my404() {
        //Code to show my 404 template
    }
}

EDIT: my URL's look like www.<sitename>.com/<languagesegment>/<optionalcityname>/<pathcontroller&function>

If the optionalcityname is filled in in the URL, the entire website is branded in the cities colors and logo etc. If it is left out, the "normal" site appears.

When going to the "normal" site, my 404 page gets shown correctly.

I've never used CodeIgniter but a quick look at the documentation seems to suggest that, inexplicably, they don't use namespaces. This is pretty unheard of for a modern codebase, and you're learning why.

PHP 7.0 introduced the Error class as the base class for all internal PHP errors. Since your code is not namespaced, you're trying to overwrite this built-in class. If you looked at your error log you'd see something like this:

PHP Fatal error:  Cannot declare class Error, because the name is already in use in...

Rename your Error class to something that isn't a reserved name and it should get you past this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM