PHP Warning Deprecated: Creation of dynamic property is deprecated

PHP Warning Deprecated: Creation of dynamic property is deprecated

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

Stefan Bogdanescu

Founder & Senior Architect

PHP Warning Deprecated: Creation of dynamic property is deprecated

If you are running PHP 8.4 or later, you have likely encountered the Deprecated: Creation of dynamic property ... is deprecated warning. This error appears whenever a class receives an attribute assignment that does not correspond to a pre-declared property. In the scenario provided, attempting to set $this->database and $this->server within the constructor triggers this behavior because those properties were never defined in the class signature.

This is not merely a cosmetic nuisance; it is a deliberate shift in how PHP handles object state. The engine now strictly enforces static typing for class properties to prevent accidental overwrites of instance data and to improve debugging clarity. When you pass an associative array to a constructor expecting flexible property injection, as shown in the example below, PHP 8.4 refuses to silently create new properties on the fly.

class database {
    public $username = "root";
    public $password = "password";
    public $port = 3306;

    public function __construct($params = array())
    {
        foreach ($params as $key => $value) {
            $this->{$key} = $value;
        }
    }
}

$db = new database(array(
    'database' => 'db_name',
    'server' => 'database.internal',
));

The code above attempts to map incoming keys like 'database' and 'server' directly onto the object. While this pattern feels convenient, it relies on dynamic property creation. In PHP 8.0 through 8.3, this triggered a notice or warning depending on configuration, but in PHP 8.4, it is explicitly deprecated to force developers to define their data contracts clearly.

Defining the Contract Explicitly

The most robust solution is to declare every potential property within the class definition. This approach aligns with modern object-oriented principles where the structure of an object is known at compile time (or parse time). By moving properties from the constructor logic into the class scope, you signal intent and prevent runtime surprises.

class database {
    public $username;
    public $password;
    public $port;
    public $database;
    public $server;

    public function __construct(array $params = [])
    {
        foreach ($params as $key => $value) {
            // Only assign if the property exists and is nullable or typed appropriately
            if (property_exists($this, $key)) {
                $this->$key = $value;
            } else {
                trigger_error("Unknown property passed: {$key}", E_USER_ERROR);
            }
        }
    }

    public function connect() {
        // Connection logic here
    }
}

In this refactored version, the properties database and server are explicitly declared. The constructor now uses property_exists() to validate that the incoming key matches a defined property before assignment. This pattern ensures that if you pass an unknown key in the future, the application fails loudly rather than silently corrupting the object state or triggering deprecation warnings.

Handling Optional Configuration

A common pitfall when refactoring this logic is assuming all properties must be declared as public and initialized with a default value. However, declaring them without initialization (as seen in the second snippet above) allows the constructor to set them dynamically while maintaining type safety. You can also leverage PHP 8's union types or nullable types if you anticipate optional parameters:

class database {
    public ?string $database = null;
    public ?string $server = null;
    // ... other properties
}

This approach provides better IDE autocomplete and static analysis support. Tools like PHPStan or Psalm can now verify that your class structure is complete before the code even runs, catching logical errors early in the development cycle.

Why This Matters for Frameworks

While this specific issue often arises from legacy patterns or custom classes, it has broader implications for framework adoption. The Laravel ecosystem, which emphasizes strict typing and clear contracts, benefits significantly from these changes. When integrating with a library that relies on dynamic properties, you might need to adjust your implementation to use typed properties or leverage features like #[\AllowDynamicProperties] (if applicable in your specific context) or simply declare the properties upfront.

For projects migrating to PHP 8.4, ignoring these deprecation warnings is no longer a viable option for long-term stability. The transition forces a cleanup of "magic" property assignments that were previously accepted as best practices but now hinder performance and tooling capabilities. Embracing explicit property declarations ensures your code remains compatible with future PHP versions and maintains the integrity of your object model.

Our network