Watch Out Dynamic Properties: Pantheon Now Offers Support for PHP 8.2

PHP 8.2 was released on December 8, 2022 and is now available on Pantheon. You can update with confidence using Pantheon’s Dev, Test, Live workflow because the PHP version is managed in version control and deployed along with the rest of your code.

We suggest using the latest PHP version to maximize your site’s security, as well as getting syntax and execution upgrades. Additionally, each PHP point release includes changes that remove ambiguity. 

Back when WordPress and Drupal were first introduced, PHP did little at the language level to discourage confusing practices, such as functions that accept and return different types of data. In the years since then, through a combination of education from the community and re-working at the language-level, it has become easier to write unambiguous code using PHP.

Dynamic properties are depreciated

While there are a number of low-level changes in 8.2 (that are unlikely to change your regular work), there is one depreciation that jumps out. For Drupal and WordPress developers, the depreciation of dynamic properties is the most notable change in PHP 8.2. This is a significant backwards compatibility break, and many folks will need to revisit older code to fix this depreciation notice. The depreciation notice will become an Error exception in PHP 9.0.

class Foo {}
$foo = new Foo;
// Deprecated: Creation of dynamic property Foo::$bar is deprecated
$foo->bar = 1;
// No deprecation warning: Dynamic property already exists.
$foo->bar = 2;

The problem here is that it can be confusing if you see a property (bar) getting set on an object, when the original declaration of the class never mentioned anything about "bar." If you're going to set and use a property on an object, you should first tell the computer, and, perhaps, more importantly your human co-workers, what that property means.

Fixing depreciation warningsAdding properties to objects dynamically isn't the end of the world. It's just a pattern of poor code hygiene that can be eliminated at the language level. But fixing the code in any given site or package might be time consuming or even risky. So there's a band-aid solution available to suppress the depreciation notices.

Any classes declared with the #[AllowDynamicProperties] attribute will not emit a depreciation notice in 8.2. For example, WordPress has already begun to address this depreciation in Core, making use of #[AllowDynamicProperties]:

class WP_Text_Diff_Renderer_inline extends Text_Diff_Renderer_inline {
     * @ignore
     * @since 2.6.0
     * @param string $string
     * @param string $newlineEscape
     * @return string
    public function _splitOnWords( $string, $newlineEscape = "\n" ) {
        $string = str_replace( "\0", '', $string );
        $words  = preg_split( '/([^\w])/u', $string, -1, PREG_SPLIT_DELIM_CAPTURE );
        $words  = str_replace( "\n", $newlineEscape, $words );
        return $words;

The better fix would be to add the property declaration to the class:

class Test {
    public $foo; // <-- Add property declaration.
    public function __construct($foo) {
        $this->value = $foo;

For more details regarding this depreciation, check out the RFC.

Interested in Upgrading?

Pantheon customers are able to make the update to PHP 8.2 by following our documentation on how to upgrade your PHP version

It’s worth noting that New Relic is not yet supported in PHP 8.2, but we hope to see a release with support in the near future. We’re keeping an eye on this GitHub issue for updates, and will test out a release when one becomes available.

If you still have questions, please reach out to support for help

Topics Development