[docs] Document the recommended use of the Override attribute in PHP#1567
[docs] Document the recommended use of the Override attribute in PHP#1567andrewnicols wants to merge 1 commit intomoodle:mainfrom
Conversation
✅ Deploy Preview for moodledevdocs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
Pull request overview
Documents the recommended use of PHP’s #[\Override] attribute in Moodle coding style guidance, and fixes a formatting/link issue in the functions naming section.
Changes:
- Add a new tip recommending
#[\Override]for methods overriding a parent class/interface/trait method, with an example. - Fix a malformed “activity modules” exception line/link in the functions naming guidance.
| When writing a method which overrides a method in a parent Class, Interface, or Trait, it is strongly recommended that the `#[\Override]` attribute be used, for example: | ||
|
|
||
| ```php title="Example of a method which overrides a third-party method" | ||
| class example extends \some\other\class { |
There was a problem hiding this comment.
The example uses \some\other\class as a parent class name, but class is a reserved keyword in PHP, so this is not a valid (or copy-pastable) class name. Please change the example to use a legal identifier for the parent class (e.g. \some\other\base_class).
| class example extends \some\other\class { | |
| class example extends \some\other\base_class { |
|
|
||
| :::tip[Overriding parent methods] | ||
|
|
||
| When writing a method which overrides a method in a parent Class, Interface, or Trait, it is strongly recommended that the `#[\Override]` attribute be used, for example: |
There was a problem hiding this comment.
This tip recommends #[\Override] without noting the minimum PHP version that supports it (PHP 8.3+). Please add a short note about the PHP version requirement (and/or Moodle versions where that PHP baseline applies) so readers targeting older supported versions don’t adopt an attribute that will fail to parse.
| When writing a method which overrides a method in a parent Class, Interface, or Trait, it is strongly recommended that the `#[\Override]` attribute be used, for example: | |
| When writing a method which overrides a method in a parent Class, Interface, or Trait, it is strongly recommended that the `#[\Override]` attribute be used. This attribute requires PHP 8.3 or later, so it should only be used in Moodle versions whose supported PHP baseline includes PHP 8.3+, for example: |
There was a problem hiding this comment.
Dearest Copilot, this attribute does not "require" PHP 8.3. Rather, it only has an effect in PHP 8.3. Earlier versions of PHP will continue to work because they ignore the attribute or interpret it as a comment. Thus, from a semantic point of view, the benefits of the attribute mean that it should be used in all versions. The primary drawback in PHP 8.3 and newer is that if the ancestor class were to remove the method that the Override attribute requires, then Fatal errors would occur. So instead of telling people to only use the attribute with newer versions of Moodle, it's more important to tell people to be careful when removing method that other code/people may have overridden. In that sense, using techniques like final for methods that should not be overridden and carefully deprecating non-final methods before outright removing them could be helpful information.
| } | ||
| ``` | ||
|
|
||
| With this attribute applied the documentation is no longer required, and code sniffer sniffs relating to both documentation, and naming of the method and its parameters are silenced. |
There was a problem hiding this comment.
“With this attribute applied the documentation is no longer required, and code sniffer sniffs … are silenced” reads as a general effect of the PHP attribute, but #[\Override] itself doesn’t affect documentation requirements or naming rules. Please reword to explicitly attribute this behavior to the Moodle coding standard/PHPCS rules (and ideally link to the relevant rule/sniff), or soften/remove the claim to avoid misleading guidance.
| With this attribute applied the documentation is no longer required, and code sniffer sniffs relating to both documentation, and naming of the method and its parameters are silenced. | |
| In Moodle's coding standard, this attribute is also used by PHPCS rules as a signal that the method is a genuine override, so some sniffs relating to documentation and to the naming of the overridden method and its parameters are not applied. |
10d77fd to
47b4fa1
Compare
No description provided.