Welcome Guest, Not a member yet? Register   Sign In
deprecated Creation of dynamic property

I'm using php 8.2 and ci 4.3.2 and i got
Creation of dynamic property App\Controllers\Document::$compteModel is deprecated

APPPATH/Controllers/Document.php at line 17

PHP Code:
11    public function initController(
12        \CodeIgniter\HTTP\RequestInterface $request,
13        \CodeIgniter\HTTP\ResponseInterface $response,
14        \Psr\Log\LoggerInterface $logger
) {
16        parent::initController($request$response$logger);        
17        $this
->compteModel = new compteModel();
18        $this->ecritureModel = new ecritureModel();

As you can see it above, in all my controllers, i put access to models in the init part to avoid writing many times
PHP Code:
[...] new someModel() 

in the different methods

I fought that, adding
protected $compteModel;

solved the issue

Is that a good way ?

Thanks, nice day !

yeah i'm on 8.2.4 I got dynamic property App\Controllers\Pages::$myDate is deprecated I messed around in my BaseController.php i put

namespace App\Controllers;
use \AllowDynamicProperties;

Im not getting errors in Dev local host apache but read

use \AllowDynamicProperties;   // this one in namespace area  

#[\AllowDynamicProperties]   // above class

#[\ReturnTypeWillChange]   /above class method
I use Arch Linux by the way 

CMS in Action

(03-21-2023, 03:58 AM)foxbille Wrote: I fought that, adding
protected $compteModel;

solved the issue

Is that a good way ?

Yes, defining the missing property is a good way!

Adding #[\AllowDynamicProperties] is not a good way in general.

Hi all,
Thanks for those replies.
There's a third way, according to https://github.com/squizlabs/PHP_CodeSni...ssues/3489
Add the magic __get() and __set() methods to ...
.. to what ?
So I keep my solution
Beside, i don't really understand what all that means... but I'm only a(n old) hobby developper fighting to find a way to make app as good as possible.

It is a simple story.
If you design that a class has a property, you should define the property.
Then devs who read the source code, they know the property is there.
If they typo the property name, PHP would report the error.

PHP Code:
<?php // https://wiki.php.net/rfc/deprecate_dynamic_properties
class User {
    public $name;
$user = new User;
// Assigns declared property User::$name.
$user->name "foo";
// Oops, a typo:
$user->nane "foo"

If you cannot specify property names for a class in advance, you use magic __get() and __set().

see here

The question is whether this is the recommended solution of the codeigniter team?

nice article @scalar1 ; it gives three approaches (if i can count) .

I went for "Exempted Dynamic Property Patterns' i.e get rid of annoying notices straight away, then apply a more appropriate fix when i have time
I use Arch Linux by the way 

CMS in Action

I have never gotten that warning message, but then I have a habit of always defining all of my Properties.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )

Theme © iAndrew 2016 - Forum software by © MyBB