Ffile2fix
Sign in Get started

Fix "Creation of dynamic property is deprecated" in PHP 8.2

Deprecated: Creation of dynamic property App\Models\User::$fullName is deprecated in /var/www/html/src/Models/User.php on line 18

Since PHP 8.2, assigning a property that the class never declared, like $this->fullName = ..., triggers a deprecation notice. The code still works for now, but the notice fills logs and may show on screen. Fix it by declaring the property in the class, or mark the class with #[\AllowDynamicProperties] if it really needs dynamic properties.

Also appears as: PHP Deprecated: Creation of dynamic property WP_Widget_Custom::$settings is deprecated Β· Deprecated: Creation of dynamic property Foo::$bar is deprecated in Β· Uncaught Error: Cannot create dynamic property Foo::$bar

Common causes

  • A class assigns $this->something without declaring public, protected or private $something
  • Code sets properties on another object from outside the class
  • Older plugins, themes or libraries written before PHP 8.2
  • A typo in a property name, so a new dynamic property is created instead of updating the declared one
  • Readonly classes and enums, where a dynamic property throws an Error ('Cannot create dynamic property') instead of a notice

How to fix it

  1. Declare the property. Add the property to the class body, for example public ?string $fullName = null;. This is the proper fix and also helps IDEs and static analysis.
  2. Check for typos. Compare the property name in the message with the declared ones. A misspelled name such as $this->usrname creates a new property and is a real bug.
  3. Allow dynamic properties where intended. If the class is a flexible data container, add #[\AllowDynamicProperties] on the line above the class keyword. Classes that extend stdClass also allow them without the attribute.
  4. Use magic methods or an array. Implement __get() and __set() backed by a private array, or store arbitrary data in a property like private array $data = [];. Classes with __set() do not raise the deprecation.
  5. Update third-party code. If the file is in a plugin, theme or vendor folder, update it to a version that supports PHP 8.2. Do not edit vendor files directly, because updates overwrite them.
  6. Keep notices off-screen in production. Set display_errors = Off and log_errors = On. In WordPress, keep WP_DEBUG_DISPLAY false on live sites so deprecations only reach the log.

Three ways to fix it

<?php
// 1. Declare the property (preferred)
class User {
    public ?string $fullName = null;
}

// 2. Explicitly allow dynamic properties
#[\AllowDynamicProperties]
class LegacyConfig {}

// 3. Use stdClass for free-form data
$row = new stdClass();
$row->anything = 'ok';

How to stop it happening again

  • Declare all properties in class definitions
  • Run PHPStan or Psalm, which flag access to undeclared properties
  • Test against new PHP versions with error_reporting = E_ALL in development
  • Scan legacy code for PHP 8.2 compatibility before upgrading

Frequently asked questions

Will dynamic properties stop working completely?

The deprecation in PHP 8.2 signals that they may become an error in a future major version. Classes with #[\AllowDynamicProperties], stdClass and classes with __set() are excluded.

Is this deprecation breaking my site?

No, a deprecation is only a notice and the code still runs. It can cause problems indirectly if notices are printed before headers are sent, so keep display_errors off in production.

Why does WordPress core show this notice?

Recent WordPress versions declare their properties or use the attribute, so core notices usually mean an outdated version. Most remaining notices come from plugins and themes that need updates.