-
Task
-
Resolution: Done
-
Critical
-
None
-
4.2
-
MOODLE_402_STABLE
-
-
6
-
Team Hedgehog Sprint 1 review, Team Hedgehog Sprint 2.1, Team Hedgehog Sprint 2.2, Team Hedgehog 2023 Sprint 1.3, Team Hedgehog 2023 Sprint 1.4, Team Hedgehog 2023 Sprint 2.0, Team Hedgehog 2023 Sprint 2.1, Team Hedgehog 2023 Sprint 2.2
From PHP 8.2, if someone tries to set a value for a class property which does not have a definitino, a deprecation notice will be emitted. For example:
class User {
|
private int $uid;
|
}
|
|
$user = new User();
|
$user->name = 'Foo';
|
Deprecated: Creation of dynamic property User::$name is deprecated in ... on line ...
|
Also from internally:
class User {
|
public function __construct() {
|
$this->name = 'test';
|
}
|
}
|
|
new User();
|
Deprecated: Creation of dynamic property User::$name is deprecated in ... on line ...
|
Much more detail here: https://php.watch/versions/8.2/dynamic-properties-deprecated
We should look at writing a sniff for this if possible.
Note: For some legacy code, we may choose to work around this using the AllowDynamicProperties attribute:
#[AllowDynamicProperties]
|
class User {
|
private int $uid;
|
}
|
|
$user = new User();
|
$user->name = 'Foo';
|
https://php.watch/versions/8.2/dynamic-properties-deprecated#AllowDynamicProperties
Other cases not affected
- stdClass
- classes with a magic __get and __set method
phpstan configuration
I used the following phpstan configuration to genreate the above:
parameters
|
# Dynamic properties emits warnings from PHP 8.2 onwards.
|
checkDynamicProperties: true
|
Note: I would recommend generating a baseline first, then adding the above configuration, and then running again against the baseline with the new configuration. This will identify any problem areas.