Skip to content

[docs] Document the recommended use of the Override attribute in PHP#1567

Open
andrewnicols wants to merge 1 commit intomoodle:mainfrom
andrewnicols:overrideAttribute
Open

[docs] Document the recommended use of the Override attribute in PHP#1567
andrewnicols wants to merge 1 commit intomoodle:mainfrom
andrewnicols:overrideAttribute

Conversation

@andrewnicols
Copy link
Copy Markdown
Member

No description provided.

Copilot AI review requested due to automatic review settings April 14, 2026 04:50
@netlify
Copy link
Copy Markdown

netlify bot commented Apr 14, 2026

Deploy Preview for moodledevdocs ready!

Name Link
🔨 Latest commit 47b4fa1
🔍 Latest deploy log https://app.netlify.com/projects/moodledevdocs/deploys/69de0422b4ac4500080d083f
😎 Deploy Preview https://deploy-preview-1567--moodledevdocs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
class example extends \some\other\class {
class example extends \some\other\base_class {

Copilot uses AI. Check for mistakes.

:::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:
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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:

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“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.

Suggested change
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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants