Issue when enabling PHP 8.x

I have the issue with my app written in Yii2 2.0.47 (same in 46).

When I enable PHP 8.0 or 8.1 on the server I start getting huge amount of errors like the below one:

  • [Tue Nov 29 16:21:18.880199 2022] [proxy_fcgi:error] [pid 366:tid 140086160824064] [client 87.206.124.190:0] AH01071: Got error ‘PHP message: PHP Warning: Attempt to read property “imie” on null in /home/test/ftp/management_tools/views/layouts/main.php on line 31PHP message: PHP Warning: Attempt to read property “nazwisko” on null in /home/test/ftp/management_tools/views/layouts/main.php on line 32’, referer: https://example.com/login

The expected properties are in main.php and are referred from Yii::$app->user object.

The funny thing is that main.php layout is set as default one but for login page I use different one:

public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        }

        $model->password = '';
        $this->layout = 'login';
        return $this->render('login', [
            'model' => $model,
        ]);
    }

This app was written in PHP 5 but I kept upgrading it till I reached 8 :wink: Almost… :stuck_out_tongue:
Yii was upgraded each time from very old 2.0.1 version as I remember.

Any hints?

It works perfectly fine with PHP 7.4 but my hosting company wants me to change it to 8+ which is ok.

Hello @klysiak . I had a similar problem on different controllers when I’ve changed to php 8.x.
It seems that this version of PHP handles those warning a bit differently than the previous ones (or it’s just a matter of configuring it to ignore that, I don’t know.

The problem you are facing is that probably you have something similar to

$object->nazwisko and
$object->imie

somewhere in your code, and that $object is empty. So the error is telling that those properties ( imie and nazwisko) they don’t exist on null, because the object itself is empty. Probably it’s a model object.

But the error is doesn’t seems to be directly related with the piece of code you posted, but maybe in the main.php has some more clues.

1 Like