PHP Creation of dynamic property is deprecated, AllowDynamicProperties not working

PHP Creation of dynamic property is deprecated, AllowDynamicProperties not working

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Trap of Inherited Dynamic Properties in PHP 8.2

When upgrading a codebase to PHP 8.2, the introduction of #[AllowDynamicProperties] was intended as a lifeline for legacy libraries that rely on dynamic property assignment. However, a common pitfall emerges when dealing with inheritance chains: the attribute only protects the specific class it is defined upon, not its children. If you are encountering [E_DEPRECATED] Creation of dynamic property ... is deprecated errors on child classes despite decorating parent classes with #[AllowDynamicProperties], the issue lies in how PHP scopes this attribute and the mechanics of property visibility inheritance.

The core misunderstanding often stems from assuming that the attribute propagates down the class hierarchy. It does not. When you assign a dynamic property to an instance of MyApiClient, PHP checks whether that specific class has allowed dynamic properties via its own definition or via a parent class's explicit permission. In your scenario, MyApiClient extends ApiClient, which extends Configuration. Even if Configuration and ApiClient are decorated with #[AllowDynamicProperties], the attribute does not automatically grant that permission to MyApiClient.

Consider this structure where the error persists despite decoration:

class Configuration { }
class ApiClient extends Configuration { }

// This allows dynamic properties for 'ApiClient' instances only.
#[AllowDynamicProperties]
class MyApiClient extends ApiClient { }

When you instantiate MyApiClient and access a non-existent property, PHP looks at MyApiClient. It sees no #[AllowDynamicProperties] attribute attached directly to it. Consequently, it falls back to the default behavior: throwing a deprecation warning for dynamic properties in strict mode (which is enabled by default in PHP 8.2+). The attributes on the parent classes are effectively ignored for the child instance because they do not define the class's own allowance for dynamic state.

The solution requires explicit decoration on every class in the chain that intends to accept dynamic properties, or a declaration of the property itself in the most derived class. If you must support a third-party library where you cannot modify the source code directly, you face a limitation: you cannot retroactively apply #[AllowDynamicProperties] to a child class if the parent chain does not explicitly pass that allowance down.

However, there is a workaround involving the __construct() method and explicit property declaration. If you can define the property in the child class, the dynamic assignment error vanishes because the property now exists statically:

class MyApiClient extends ApiClient
{
    public $propertyName; // Explicitly declared property removes deprecation
    
    public function __construct() {
        $this->propertyName = 'foo';
    }
}

This approach works because PHP no longer treats the assignment as creating a dynamic property; it is simply assigning a value to an existing one. While this might feel redundant, it is the robust way to handle dynamic properties in strict environments when you cannot control the parent class definitions.

If you are wrapping a third-party library and cannot edit its files, you have two main paths forward:

  1. Explicit Declaration: Manually declare every property that the library expects to be set dynamically in your wrapper classes. This ensures compatibility with PHP 8.2's strict mode without relying on attributes that might not propagate as expected.
  2. Attribute Injection: If you can create a proxy or adapter class, you must ensure #[AllowDynamicProperties] is applied to the specific class where the dynamic assignment occurs.

The confusion often arises from the expectation that PHP would "inherit" the permission flag. It treats #[AllowDynamicProperties] as a class-level modifier similar to public, protected, or private. Since child classes do not inherit visibility modifiers for attributes in the same way they inherit methods, you must be explicit on every level where dynamic properties are created.

For long-term compatibility with libraries that were written before PHP 8.2, relying on explicit property declarations is often safer than relying on attributes that might break inheritance assumptions. This ensures your application remains stable regardless of the PHP version or strictness settings configured in php.ini.

Our network