Showing entries with tag "deprecated".

Found 1 entries

PHP: Creation of dynamic property Class::$Var is deprecated

I upgraded to PHP 8.2 today and started getting a lot of Creation of dynamic property Class::$Var is deprecated errors. This is just PHP's way of telling you that you need to pre-declare your class variables before you use them. To me this just meant adding a lot of:

class user {
    var $name  = "";
    var $id    = 0;
    var $perms = [];

to the tops of my classes. This is the right way to fix things, and what I did in 99% of the cases where I was seeing this error.

However I do have a couple of classes where it's necessary to be able to add dynamic properties on the fly, and PHP does offer a work around for this. If you use a special declaration immediately before your class statement PHP will allow dynamic properties to be added without throwing any errors.

#[\AllowDynamicProperties]
class user {

Note: The \ in the brackets is required

Leave A Reply