Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Deprecated: Creation of dynamic property in PHP 8.2 and later

ErrorDeprecated: Creation of dynamic property Foo::$bar is deprecated

Short answer

PHP 8.2 deprecates assigning to a property the class never declared; PHP 9.0 will make it an Error. Declare the property in the class. If a legacy or vendor class must keep dynamic properties, add the #[\AllowDynamicProperties] attribute. To attach extra data to an object you do not own, use a WeakMap.

  • Applies to PHP 8.2, PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    The message names the class and the property. Find where that property is first assigned and check whether the class declares it.

    grep -rn "class Foo" src/
    grep -rn -- "->bar =" src/
  2. 2Match what you see, then jump to its fix

    1. 1Deprecated: Creation of dynamic property Foo::$bar is deprecated, and the class is your own codeThe property is assigned but never declaredFix
    2. 2The class sits in vendor/ or is a base class with many children, and declaring every property is not practicalA legacy or third-party class relies on dynamic propertiesFix
    3. 3The deprecation names a built-in or vendor class, and your code adds a cache key or flag to itExtra data is attached to an object you do not ownFix

What does the error look like?

  • Deprecated: Creation of dynamic property Foo::$bar is deprecated

Cause 1 of 3

The property is assigned but never declared

1Check

Open the class and look for a declaration of the property named in the message. A typo in the name creates a second, dynamic property, so compare spelling too.

grep -n 'bar' src/Foo.php

2Fix

Declare the property, with a type if you know it. This is the fix the PHP manual prefers.

class Foo
{
    public ?string $bar = null;
}

3Verify

Run the code path again with error_reporting set to E_ALL; the deprecation should be gone.

php -d error_reporting=E_ALL -d display_errors=1 script.php

Confirmed by php.net

Did this fix it?

Cause 2 of 3

A legacy or third-party class relies on dynamic properties

1Check

Confirm the class is not stdClass and does not handle the property through __get()/__set(); those two are not affected by the change.

grep -n '__get\|__set\|AllowDynamicProperties' vendor/vendor-name/package/src/Foo.php

2Fix

Add the attribute to the class. It also applies to every child class. For a vendor package, update it first, since the maintainers usually fix this themselves.

#[\AllowDynamicProperties]
class LegacyModel
{
}

3Verify

The deprecation no longer appears for LegacyModel or its children.

php -d error_reporting=E_ALL -d display_errors=1 script.php

Confirmed by wiki.php.net

Did this fix it?

Cause 3 of 3

Extra data is attached to an object you do not own

1Check

Look for lines that set a new property on an object that came from a library.

grep -rn '\$[a-z]*->myFlag' src/

2Fix

Keep the extra data in a WeakMap keyed by the object. The entry disappears when the object is freed.

$meta = new WeakMap();
$meta[$user] = ['cacheKey' => 'abc'];
echo $meta[$user]['cacheKey'];

3Verify

Run the script; no property is created on $user and no deprecation is raised.

php -d error_reporting=E_ALL -d display_errors=1 script.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Declare every property a class uses, including the ones set by a constructor or a hydrator.
  • Run the test suite with error_reporting set to E_ALL and treat deprecations as failures in CI.
  • Use stdClass or an array when you really need a free-form bag of values.
  • Update third-party packages before silencing anything.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.