You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[docs] Add class properties deprecation guidelines
Documents the process for deprecating class properties, which cannot
emit runtime warnings in PHP < 8.4.
Key points:
- Private properties: remove immediately
- Protected properties in non-extensible classes: can be removed
- Public/protected properties in extensible classes: use @deprecated
tags, attributes, and provide getter/setter methods with warnings
- From Moodle 6.0 (PHP 8.4+): use property hooks for proper deprecation
Copy file name to clipboardExpand all lines: general/development/policies/deprecation/index.md
+105Lines changed: 105 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -205,6 +205,111 @@ A code removal step was added to the deprecation process in Moodle 5.0 and is al
205
205
- Functions deprecated in Moodle 4.5 (LTS) will be up for final deprecation in Moodle 6.0 (the first release for Series 6 right after the Moodle 5.3 (LTS) release), and for removal in Moodle 7.0 (the first Series 7 Moodle version).
206
206
</ValidExample>
207
207
208
+
## Class properties deprecation
209
+
210
+
Deprecating class properties presents unique challenges compared to methods, as PHP versions before 8.4 do not provide a native mechanism to emit warnings when properties are accessed or modified.
211
+
212
+
### When can a property be deprecated?
213
+
214
+
The approach to deprecating a property depends on its visibility and usage:
215
+
216
+
1.**Private properties**: Can be removed immediately as they are not part of the public API.
217
+
218
+
2.**Protected properties in non-extensible classes**: Can be removed if there is no reasonable expectation that external code extends the class. This applies to classes where:
219
+
- The API is not designed to be extended
220
+
- It would be extremely difficult for developers to correctly extend and consume the class
221
+
- The protected property is not considered part of the 'public' API
222
+
223
+
3.**Public properties, or protected properties in extensible classes**: Cannot be safely removed using the standard deprecation process, as there is no way to alert developers when the property is accessed in PHP versions before 8.4.
224
+
225
+
### Deprecation process for properties that cannot be removed
226
+
227
+
For public properties and protected properties in extensible classes, follow these steps:
228
+
229
+
1.**Add the `@deprecated` PHPDoc tag** to the property documentation, including:
230
+
- The version it was deprecated in.
231
+
- The reason for deprecation.
232
+
- Alternative approaches or replacement properties.
233
+
234
+
```php
235
+
/**
236
+
* @var string
237
+
* @deprecated since 4.4 Use get_summary() method instead
238
+
*/
239
+
public string $summary;
240
+
```
241
+
242
+
2.**Document in upgrade notes**: Ensure the deprecation is clearly documented in the [upgrade notes](../../upgradenotes.md) for the release.
243
+
244
+
3.**Add the `\core\attribute\deprecated` attribute**:
245
+
246
+
```php
247
+
#[\core\attribute\deprecated(
248
+
since: '4.4',
249
+
reason: 'Direct property access is deprecated. Use get_summary() and set_summary() methods instead.',
250
+
mdl: 'MDL-XXXXX',
251
+
)]
252
+
public string $summary;
253
+
```
254
+
255
+
4.**Ensure there are no internal uses**: Remove all usage of the property within Moodle core code, replacing it with the recommended alternative (such as getter/setter methods).
256
+
257
+
5.**Provide getter/setter methods**: If the property previously had direct access, create getter and setter methods that:
258
+
- Provide the same functionality.
259
+
- Can emit deprecation warnings when the underlying property is accessed.
260
+
- Serve as the migration path for developers.
261
+
262
+
```php
263
+
/**
264
+
* Get the summary text.
265
+
*
266
+
* @return string
267
+
*/
268
+
public function get_summary(): string {
269
+
return $this->summary;
270
+
}
271
+
272
+
/**
273
+
* Set the summary text.
274
+
*
275
+
* @param string $summary The summary text
276
+
*/
277
+
public function set_summary(string $summary): void {
278
+
if ($summary !== $this->summary) {
279
+
debugging(
280
+
'Direct property access to $summary is deprecated. Use set_summary() instead.',
281
+
DEBUG_DEVELOPER
282
+
);
283
+
}
284
+
$this->summary = $summary;
285
+
}
286
+
```
287
+
288
+
### Property deprecation from Moodle 6.0 onwards
289
+
290
+
From Moodle 6.0, the minimum supported PHP version will be 8.4. PHP 8.4 introduces [Property Hooks](https://wiki.php.net/rfc/property-hooks), which allow proper deprecation warnings to be emitted when properties are accessed or modified.
291
+
292
+
**Example using PHP 8.4 property hooks:**
293
+
294
+
```php
295
+
#[\core\attribute\deprecated(
296
+
since: '4.4',
297
+
reason: 'The "summary" attribute on the "table" element is not supported in HTML5. ' .
298
+
'Consider describing the structure of the table in a "caption" element or in a ' .
299
+
'"figure" element containing the table; or, simplify the structure of the table ' .
This allows Moodle to emit proper runtime deprecation warnings when properties are written to, providing the feedback mechanism that is currently unavailable in earlier PHP versions.
312
+
208
313
## Parameters deprecation
209
314
210
315
Whilst it is possible to deprecate individual method parameters, care must be taken in doing so.
0 commit comments