CodeIgniter Forums
Creation of dynamic property DEPRECATED - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Creation of dynamic property DEPRECATED (/showthread.php?tid=92223)



Creation of dynamic property DEPRECATED - Vespa - 12-25-2024

To create a multilanguages web site I've added into the initController method in BaseController file the following code that causes a warning in log :
Severity Message
info Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
warning [DEPRECATED] Creation of dynamic property App\Controllers\Home::$session is deprecated in APPPATH\Controllers\BaseController.php on line 68

What's wrong and how to fix it? Thanks

Code:
    if (session_status() == PHP_SESSION_NONE) {
        $this->session = \Config\Services::session();
    } 



RE: Creation of dynamic property DEPRECATED - JustJohnQ - 12-25-2024

Add this line to your BaseController.php:

PHP Code:
protected $session
You also might want to read this:
https://php.watch/versions/8.2/dynamic-properties-deprecated


RE: Creation of dynamic property DEPRECATED - InsiteFX - 12-26-2024

This works for me in the BaseController.

PHP Code:
use Config\Services;

/**
 * E.g.: $this->session = \Config\Services::session();
 * Ensure that the session is started and running
 */
if (session_status() == PHP_SESSION_NONE) {
    $this->session Services::session();




RE: Creation of dynamic property DEPRECATED - Vespa - 12-26-2024

Thanks a lot for feedback guys...still lots to learn about CI4....


RE: Creation of dynamic property DEPRECATED - JustJohnQ - 12-26-2024

(12-26-2024, 12:09 AM)InsiteFX Wrote: This works for me in the BaseController.

PHP Code:
use Config\Services;

/**
 * E.g.: $this->session = \Config\Services::session();
 * Ensure that the session is started and running
 */
if (session_status() == PHP_SESSION_NONE) {
    $this->session Services::session();


But you still explicitly declare $session as a property?


RE: Creation of dynamic property DEPRECATED - InsiteFX - 12-27-2024

Yes, but I don't use it, I just make sure it's loaded.
I use the session help like so:

PHP Code:
session()->set($data);

$name session()->get('name');

// etc;