PHP 8.2 Dynamic Properties Deprecated: how to use them anyway in a compatible way

PHP 8.2 Dynamic Properties Deprecated: how to use them anyway in a compatible way

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

Stefan Bogdanescu

Founder & Senior Architect

PHP 8.2 Dynamic Properties Deprecated: how to use them anyway in a compatible way

As of PHP 8.2, creating dynamic properties on classes is deprecated. Attempting to assign a value to a property that does not exist in the class definition will trigger an E_DEPRECATED warning. By default, this deprecation level often halts execution or at least floods your logs, but the ultimate fate of this feature is sealed: starting with PHP 9, this behavior will result in a fatal error.

While the RFC explicitly frames dynamic properties on classes as poor object-oriented practice—encouraging explicit interfaces and data classes instead—the reality of legacy codebases often tells a different story. You are likely maintaining a project where stdClass objects or existing models rely on dynamic property assignment for polymorphism, lazy loading, or specific library behaviors. The goal here isn't to advocate reverting to bad practices, but to provide a concrete path forward for code that depends on this mechanism without breaking under the new deprecation rules.

Understanding the Warning Mechanism

The core issue is how PHP handles the E_DEPRECATED error level. In many environments, including default configurations for newer versions, deprecated warnings are treated as fatal errors immediately upon occurrence. If your application throws an object and checks is_object($obj), expecting properties to be added later, you will crash before reaching that logic.

To bridge this gap, you must explicitly tell PHP to ignore these specific deprecation notices during the runtime of the script. The standard approach involves utilizing the error handler stack or modifying the error reporting constants at the very start of your execution.

You can suppress these warnings by adding a custom error handler at the top of your entry point file (like index.php or public/index.php) before any logic that triggers the dynamic property creation runs:

<?php
// index.php

$old_handler = set_error_handler(function ($errno, $errstr, $errfile, $errline) {
    // Do not report E_DEPRECATED warnings related to dynamic properties
    if (defined('E_USER_DEPRECATED') && $errno === E_USER_DEPRECATED) {
        return true;
    }
    
    // Pass other errors to the default handler
    return false;
});

// Or, more simply using error_reporting flags
error_reporting(E_ALL & ~E_DEPRECATED);

// Now proceed with your application logic
$obj = new stdClass();
$obj->dynamic_field = 'value'; // This will no longer throw a fatal error in 8.2

However, relying solely on error_reporting might not be sufficient if your framework or specific libraries treat E_DEPRECATED as an immediate stopper regardless of the flag. A more robust method for full compatibility involves setting up a custom handler that specifically filters out the E_DEPRECATED notice while allowing other critical errors to bubble up.

Implementing a Custom Handler

If you are working within a framework like Laravel or a custom application where global error reporting changes are restricted, you can register a specific handler that intercepts the deprecation events. This ensures that the logic remains intact even if the underlying PHP configuration flags change in production environments.

<?php
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
    // Ignore E_DEPRECATED specifically for dynamic properties
    if ($errno === E_USER_DEPRECATED) {
        return true; 
    }
    
    // Log other errors or let them crash the script as usual
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});

This pattern ensures that your code continues to function on PHP 8.2 while gracefully ignoring the deprecation warnings associated with dynamic properties. It prepares you for the inevitable transition to PHP 9, where these operations will stop working entirely unless you refactor the underlying architecture.

The Road Ahead

Suppressing the warning is a temporary fix designed to maintain compatibility. It allows your application to continue running on PHP 8.2 and prevents immediate crashes when upgrading to PHP 9 without refactoring. However, it is crucial to remember that this suppression does not solve the architectural debt.

If you plan to move forward with modernizing your stack, the ultimate solution remains migrating away from dynamic properties on classes. This might involve replacing stdClass usage with typed properties, utilizing data transfer objects (DTOs), or leveraging Laravel's built-in models which handle attributes explicitly. If you are using a service provided by LaravelCompany, they offer migration tools and documentation that can assist in transitioning legacy dynamic property patterns into typed structures without disrupting your deployment pipeline.

Ultimately, suppressing the error buys you time. It lets you finish the current release cycle or complete a major project before tackling the necessary refactoring to align with strict object-oriented standards required for future PHP versions.

Our network