[Solved] Old WordPress code got "creation of dynamic property is deprecated" warning – WordPress

by
Ali Hasan
composer-php wordpress

The Solutions:

Solution 1: Declare Dynamic Properties Before Use

Starting from PHP 8.2, dynamic properties in objects are deprecated. To resolve the "Creation of dynamic property is deprecated" warning, you can declare the $MyStyle property before using it in the JSXStyle class:

class JSXStyle {

  // Declare the $MyStyle property before use.
  protected string $MyStyle;

  public function __construct( $name ) {
    $this->MyStyle = $name;
    add_action( 'wp_enqueue_scripts', array( $this, 'register_custom_style' ) );
  }

  public function register_custom_style() {
    wp_enqueue_style( $this->MyStyle, get_stylesheet_directory_uri() . "/css/{$this->MyStyle}.css", array(), '4.0.0' );
  }
}

Q&A

How to fix the &quotcreation of dynamic property is deprecated" error in old WordPress code?

Declare the property before use.

What is the &quotcreation of dynamic property is deprecated" error in WordPress?

Starting from WP 8.2, creating properties in objects is deprecated.

Video Explanation:

The following video, titled "”How", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

”How

… Code Canvas•699 views.”]