Welcome Guest, Not a member yet? Register   Sign In
Migrating to php 8.2 - how to solve deprecated dynamics properties
#1

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-dyna...-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

Reply
#2

Try to add:
PHP Code:
use AllowDynamicProperties

But the best way is to define the properties you use.
Because dynamic properties are deprecated.
Reply
#3
Thumbs Up 

(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
Reply
#4

(This post was last modified: 01-07-2023, 01:27 PM by volkankaban.)

(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?
Reply
#5

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

(This post was last modified: 01-16-2023, 10:45 AM by jasonzig.)

(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`

(Using CodeIgniter since 1.x)
Reply




Theme © iAndrew 2016 - Forum software by © MyBB