Skip to content

Commit 47b4fa1

Browse files
committed
[docs] Document the recommended use of the Override attribute in PHP
1 parent 3210175 commit 47b4fa1

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

  • general/development/policies/codingstyle

general/development/policies/codingstyle/index.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,23 @@ In the case of legacy functions (those not placed in classes), names should star
268268

269269
Verbosity is encouraged: function names should be as illustrative as is practical to enhance understanding.
270270

271+
:::tip[Overriding parent methods]
272+
273+
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:
274+
275+
```php title="Example of a method which overrides a third-party method"
276+
class example extends \Some\Vendor\ExampleClass {
277+
#[\Override]
278+
public function makeRequest(): void {
279+
// ...
280+
}
281+
}
282+
```
283+
284+
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.
285+
286+
:::
287+
271288
The uses of type hints and return type declarations is required in PHP in all possible locations for all new code. There will be necessary exclusions, such as code extending existing non-compliant code and implementing things where it is not available. Progressive approach will be applied.
272289

273290
:::note
@@ -286,7 +303,7 @@ function report_participation_get_overviews(string $action, ?int userid): ?array
286303

287304
</ValidExample>
288305

289-
There is an exception for [activity modules](/docs/apis/plugintypes/mod) modules|activity modules]] that still use only plugin name as the prefix for legacy reasons.
306+
There is an exception for [activity modules](/docs/apis/plugintypes/mod) that still use only plugin name as the prefix for legacy reasons.
290307

291308
<ValidExample>
292309

0 commit comments

Comments
 (0)