Congratulations! 😍 As a blog reader, get a 10% OFF on WP Frontend Delete Account PRO. Use coupon code WPFDA10OFF.

Since PHP 8.2, adding a new property to an object at runtime is deprecated, and such that you shouldn’t do this anymore.

Example:

class MyPlugin {

     public function __construct() {
            $this->items = 10; // Creating property at runtime.
     }
}

When you do this, the property $tiems is dynamically created as untyped public properties, but in the future, you will need to declare them explicitly. With PHP 8.2, it throws a deprecation message.

[08-Aug-2023 05:53:22 UTC] PHP Deprecated:  Creation of dynamic property Plugin::$items is deprecated in C:\laragon\www\dev\wp-content\plugins\my-plugin\my-plugin.php on line 7.

Fix:

1) Declare property explicitly

class MyPlugin {
     
    public $items;     

     public function __construct() {
            $this->items = 10;
     }
}

2) Suppress Error

#[AllowDynamicProperties]
class MyPlugin {
     
     public function __construct() {
            $this->items = 10; // Creating property at runtime.
     }
}

Hope this helps! 🙂

In case you don’t already know – the required parameter followed by optional parameter is also deprecated in PHP 8.

Also, worth reading ^^

[Fix] Creation of dynamic property is deprecated since PHP 8.2
Tagged on:

Sanjeev Aryal

Don't bury your thoughts, put your vision into reality ~ Bob Marley.

Leave a Reply

Your email address will not be published. Required fields are marked *