PHP 8.2 Dynamic Properties are deprecated
See https://wiki.php.net/rfc/deprecate_dynamic_properties.
I see this as a big one, which could require a significant number of fixes for CiviCRM. The RFC has a good example showing what has changed:
class User {
public $name;
}
$user = new User;
// Assigns declared property User::$name.
$user->name = "foo";
// Oops, a typo:
$user->nane = "foo";
// PHP <= 8.1: Silently creates dynamic $user->nane property.
// PHP 8.2: Raises deprecation warning, still creates dynamic property.
// PHP 9.0: Throws Error exception.
There are many uses of dynamic properties in CiviCRM core which fall into multiple categories:
- Known undeclared properties. For example
CRM_Activity_Form_Task_Batch
repeatedly references$_fields
which should just be defined. - Dynamic properties. The main example of this is
CRM_Core_DAO
. PHP allows for this use-case by either extendingstdClass
or using the new#[AllowDynamicProperties]
attribute. Where possible we should prefer extendingstdClass
which is likely to have better long-term support (i.e. into the PHP 9.x series) - Typo's etc. Cases where the property is defined, but later referenced with a different name. These are easy; we just fix the typo (whilst ensuring we don't break any backwards compatiability for plugins using the wrong spelling)
I suggest CiviCRM developers should start to be made aware of this now, to avoid making the problem worse. Incrementally starting to fix the issue, a class or two at a time, seems like a good idea.
In many cases it's not clear why dynamic properties (or declared properties for that matter) are being used instead of a standard variable. This might be a good chance to start annotating properties as either:
-
@api
; designed to be used within hooks, on singletons etc by CiviCRM extensions -
@internal
; not designed to be used by extensions, and liable to be removed without deprecation.