CodeIgniter Forums
Migrating to php 8.2 - how to solve deprecated dynamics properties - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Migrating to php 8.2 - how to solve deprecated dynamics properties (/showthread.php?tid=85893)



Migrating to php 8.2 - how to solve deprecated dynamics properties - kcs - 12-18-2022

Hi,
I am trying to upgrade to php 8.2, but running into the deprecated dynamic properties issues and I am not sure how to solve it.
In my BaseController, I prepare some elements I need within the views, but that generates the deprecated error. Here how it looks like:

PHP Code:
abstract class BaseController extends Controller
{

protected 
$request;

protected 
$helpers = [];

    public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request$response$logger);

        // Preload any models, libraries, etc, here.
        helper('html');

        // E.g.: $this->session = \Config\Services::session();
        $this->viewData['locale'] = $request->getLocale();
        $this->viewData['supportedLocales'] = $request->config->supportedLocales;
        $this->viewData['currentMenu'] = '';
        $this->viewData['axeptioID'] = getenv('axeptioID');
        $this->viewData['axeptioCookiesVersion'] = getenv('axeptioCookiesVersion');

        $this->session = \Config\Services::session();
        $this->session->set('locale',$request->getLocale());


    }



I tried adding as suggested here https://stitcher.io/blog/deprecated-dynamic-properties-in-php-82 the built-in attribute, but without success.

PHP Code:
#[AllowDynamicProperties]
abstract class BaseController extends Controller
{
... 

{What is the best way to solve this? Thanks for your help




RE: Migrating to php 8.2 - how to solve deprecated dynamics properties - kenjis - 12-18-2022

Try to add:
PHP Code:
use AllowDynamicProperties

But the best way is to define the properties you use.
Because dynamic properties are deprecated.


RE: Migrating to php 8.2 - how to solve deprecated dynamics properties - kcs - 12-19-2022

(12-18-2022, 05:47 PM)kenjis Wrote: Try to add:
PHP Code:
use AllowDynamicProperties

But the best way is to define the properties you use.
Because dynamic properties are deprecated.

Use AllowDynamicProperties still does not work (or maybe I did not place it where it should for it to work. 

But I now understand what you meant by "define properties".  
PHP Code:
protected $viewData = [];
protected 
$session = []; 
solved my issue.

Thanks  Cool


RE: Migrating to php 8.2 - how to solve deprecated dynamics properties - volkankaban - 01-07-2023

(12-19-2022, 02:08 AM)kcs Wrote:
(12-18-2022, 05:47 PM)kenjis Wrote: Try to add:
PHP Code:
use AllowDynamicProperties

But the best way is to define the properties you use.
Because dynamic properties are deprecated.

Use AllowDynamicProperties still does not work (or maybe I did not place it where it should for it to work. 

But I now understand what you meant by "define properties".  
PHP Code:
protected $viewData = [];
protected 
$session = []; 
solved my issue.

Thanks  Cool

After that, I got $_SESSION errors, do I need to update system folder something like change session.name to session.id?


RE: Migrating to php 8.2 - how to solve deprecated dynamics properties - kenjis - 01-07-2023

What is the $_SESSION errors?
Can you show the exact error messages?


RE: Migrating to php 8.2 - how to solve deprecated dynamics properties - jasonzig - 01-16-2023

(12-19-2022, 02:08 AM)kcs Wrote:
(12-18-2022, 05:47 PM)kenjis Wrote: Try to add:
PHP Code:
use AllowDynamicProperties

But the best way is to define the properties you use.
Because dynamic properties are deprecated.

Use AllowDynamicProperties still does not work (or maybe I did not place it where it should for it to work. 

But I now understand what you meant by "define properties".  
PHP Code:
protected $viewData = [];
protected 
$session = []; 
solved my issue.

Thanks  Cool

This worked for me as well.

Adding
PHP Code:
protected $session = []; 
to my base controller allowed the this statement to work in the base controller initController method:
PHP Code:
$this->session = \Config\Services::session(); 

Thanks so much to kcs for the question, and to kenjis for the solution.

context: PHP 8.2.1 Development Server via homebrew + CI4.2.11 `php spark serve`