I'm working with a 3rd party composer library that doesn't currently support PHP 8.2 and trying to add compatibility to my application.
The 3rd party package has the following classes:
class Configuration { }
class ApiClient extends Configuration { }
I then have another extension:
class MyApiClient extends ApiClient { }
I am receiving the following error:
[E_DEPRECATED] Creation of dynamic property MyApiClient::$propertyName is deprecated
The fix is supposed to be adding #[AllowDynamicProperties]
, however this isn't actually doing anything.
I have the following:
#[AllowDynamicProperties]
class MyApiClient extends ApiClient { }
And still receive the deprecation error.
I have even edited the underlying library files, to end up with the following:
#[AllowDynamicProperties]
class Configuration { }
#[AllowDynamicProperties]
class ApiClient extends Configuration { }
#[AllowDynamicProperties]
class MyApiClient extends ApiClient { }
And yet the deprecation error persists.
I'm at a bit of a loss as I can see no reason the attributes wouldn't be taking effect. What am I missing? I'm definitely editing the right files, and if I define the properties inside MyApiClient
the deprecation error goes away.
Edit:
As a clearer example as one is seemingly needed:
class Configuration
{
public function __construct()
{
$this->propertyName = 'foo';
}
}
class ApiClient extends Configuration { }
class MyApiClient extends ApiClient { }
Error: [E_DEPRECATED] Creation of dynamic property MyApiClient::$propertyName is deprecated
Expected fix is to add #[AllowDynamicProperties]
:
class Configuration
{
public function __construct()
{
$this->propertyName = 'foo';
}
}
class ApiClient extends Configuration { }
#[AllowDynamicProperties]
class MyApiClient extends ApiClient { }
However, even with this attribute, the deprecation error persists.
Error: [E_DEPRECATED] Creation of dynamic property MyApiClient::$propertyName is deprecated
Even when the underlying composer library files are manually edited (which I cannot do as a long term fix) it does not work:
#[AllowDynamicProperties]
class Configuration
{
public function __construct()
{
$this->propertyName = 'foo';
}
}
#[AllowDynamicProperties]
class ApiClient extends Configuration { }
#[AllowDynamicProperties]
class MyApiClient extends ApiClient { }
Error: [E_DEPRECATED] Creation of dynamic property MyApiClient::$propertyName is deprecated
The only way to resolve the error is to define the property:
class MyApiClient extends ApiClient
{
public $propertyName;
}
However, this should not be necessary.