CodeIgniter Forums
deprecated Creation of dynamic property - 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: deprecated Creation of dynamic property (/showthread.php?tid=87158)



deprecated Creation of dynamic property - foxbille - 03-21-2023

Hello,
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
15    
) {
16        parent::initController($request$response$logger);        
17        $this
->compteModel = new compteModel();
18        $this->ecritureModel = new ecritureModel();
19    


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
Code:
protected $compteModel;

solved the issue

Is that a good way ?

Thanks, nice day !
Eric


RE: deprecated Creation of dynamic property - captain-sensible - 03-21-2023

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

Code:
namespace App\Controllers;
use \AllowDynamicProperties;

Im not getting errors in Dev local host apache but read

Code:
use \AllowDynamicProperties;   // this one in namespace area  


#[\AllowDynamicProperties]   // above class


#[\ReturnTypeWillChange]   /above class method



RE: deprecated Creation of dynamic property - kenjis - 03-21-2023

(03-21-2023, 03:58 AM)foxbille Wrote: I fought that, adding
Code:
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.


RE: deprecated Creation of dynamic property - foxbille - 03-22-2023

Hi all,
Thanks for those replies.
There's a third way, according to https://github.com/squizlabs/PHP_CodeSniffer/issues/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.
Eric


RE: deprecated Creation of dynamic property - kenjis - 03-25-2023

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().


RE: deprecated Creation of dynamic property - scalar1 - 03-26-2023

see here
https://php.watch/versions/8.2/dynamic-properties-deprecated


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


RE: deprecated Creation of dynamic property - captain-sensible - 03-26-2023

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


RE: deprecated Creation of dynamic property - InsiteFX - 03-27-2023

I have never gotten that warning message, but then I have a habit of always defining all of my Properties.