Deprecated: Creation of dynamic property in PHP 8.2 and later
Deprecated: Creation of dynamic property Foo::$bar is deprecatedShort 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.
Fix it fast
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/2Match what you see, then jump to its fix
- 1
Deprecated: Creation of dynamic property Foo::$bar is deprecated, and the class is your own codeThe property is assigned but never declaredFix - 2
The 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
The 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
- 1
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.php2Fix
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.phpConfirmed by php.net
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.php2Fix
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.phpConfirmed by wiki.php.net
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.phpConfirmed by php.net
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix