Skip to content

Commit 11042ad

Browse files
Merge pull request #22 from DaveLiddament/feature/override
ADD Override attribute
2 parents c6f59c6 + 0d96774 commit 11042ad

File tree

5 files changed

+244
-0
lines changed

5 files changed

+244
-0
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The intention, at least initially, is that these extra language features are enf
1717
- [Friend](#friend)
1818
- [NamespaceVisibility](#namespaceVisibility)
1919
- [InjectableVersion](#injectableVersion)
20+
- [Override](#override)
2021
- [Sealed](#sealed)
2122
- [TestTag](#testtag)
2223

@@ -30,6 +31,7 @@ The intention, at least initially, is that these extra language features are enf
3031
- [Friend](#friend)
3132
- [NamespaceVisibility](#namespaceVisibility)
3233
- [InjectableVersion](#injectableVersion)
34+
- [Override](#override)
3335
- [Sealed](#sealed)
3436
- [TestTag](#testtag)
3537
- Deprecated
@@ -369,8 +371,16 @@ class MyService
369371

370372
```
371373

374+
## Override
372375

376+
The `#[Override]` attribute is used to denote that a method is overriding a method in a parent class. This is the functionality is similar to the `@override` annotation in Java.
373377

378+
This is temporary until PHP 8.3 is released. See the [RFC](https://wiki.php.net/rfc/marking_overriden_methods) that will be implemented in PHP 8.3.
379+
380+
NOTE:
381+
382+
- If you are using PHP 8.3 then use the real `#[Override]` attribute.
383+
- This implementation doesn't consider traits.
374384

375385
## Sealed
376386

examples/override/overrideOnClass.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OverrideOnClass {
6+
7+
8+
use DaveLiddament\PhpLanguageExtensions\Override;
9+
10+
abstract class BaseClass {
11+
12+
abstract public function method1(): void;
13+
14+
public function method2(): void {}
15+
16+
public function anotherMethod(): void {}
17+
18+
}
19+
20+
21+
class Class1 extends BaseClass {
22+
23+
#[Override] public function method1(): void
24+
{
25+
}
26+
27+
#[Override] public function method2(): void
28+
{
29+
}
30+
31+
#[Override] public function method4(): void // ERROR
32+
{
33+
}
34+
}
35+
36+
37+
new class extends BaseClass {
38+
#[Override] public function method1(): void
39+
{
40+
}
41+
42+
#[Override] public function method2(): void
43+
{
44+
}
45+
46+
#[Override] public function method3(): void // ERROR
47+
{
48+
}
49+
};
50+
51+
}
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OverrideOnInterface {
6+
7+
8+
use DaveLiddament\PhpLanguageExtensions\Override;
9+
10+
abstract class AnInterface {
11+
12+
abstract public function method1(): void;
13+
14+
public function method2(): void {}
15+
16+
public function anotherMethod(): void {}
17+
18+
}
19+
20+
21+
class Class1 extends AnInterface {
22+
23+
#[Override] public function method1(): void
24+
{
25+
}
26+
27+
#[Override] public function method2(): void
28+
{
29+
}
30+
31+
#[Override] public function method4(): void // ERROR method4
32+
{
33+
}
34+
}
35+
36+
37+
new class extends AnInterface {
38+
#[Override] public function method1(): void
39+
{
40+
}
41+
42+
#[Override] public function method2(): void
43+
{
44+
}
45+
46+
#[Override] public function method3(): void // ERROR method3
47+
{
48+
}
49+
};
50+
51+
}
+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace overrideRfcExample1 {
6+
7+
8+
use DaveLiddament\PhpLanguageExtensions\Override;
9+
10+
class P {
11+
protected function p(): void {}
12+
}
13+
14+
class C extends P {
15+
#[Override]
16+
public function p(): void {}
17+
}
18+
}
19+
20+
namespace overrideRfcExample2 {
21+
22+
use DaveLiddament\PhpLanguageExtensions\Override;
23+
24+
class Foo implements \IteratorAggregate
25+
{
26+
#[Override]
27+
public function getIterator(): \Traversable
28+
{
29+
yield from [];
30+
}
31+
}
32+
}
33+
34+
35+
namespace overrideRfcExample5 {
36+
37+
use DaveLiddament\PhpLanguageExtensions\Override;
38+
39+
interface I {
40+
public function i();
41+
}
42+
43+
interface II extends I {
44+
#[Override]
45+
public function i();
46+
}
47+
48+
class P {
49+
public function p1() {}
50+
public function p2() {}
51+
public function p3() {}
52+
public function p4() {}
53+
}
54+
55+
class PP extends P {
56+
#[Override]
57+
public function p1() {}
58+
public function p2() {}
59+
#[Override]
60+
public function p3() {}
61+
}
62+
63+
class C extends PP implements I {
64+
#[Override]
65+
public function i() {}
66+
#[Override]
67+
public function p1() {}
68+
#[Override]
69+
public function p2() {}
70+
public function p3() {}
71+
#[Override]
72+
public function p4() {}
73+
public function c() {}
74+
}
75+
}
76+
77+
namespace overrideRfcExample6 {
78+
79+
use DaveLiddament\PhpLanguageExtensions\Override;
80+
81+
class C
82+
{
83+
#[Override] public function c(): void {} // ERROR c
84+
}
85+
}
86+
87+
namespace overrideRfcExample7 {
88+
89+
use DaveLiddament\PhpLanguageExtensions\Override;
90+
91+
interface I {
92+
public function i(): void;
93+
}
94+
95+
class P {
96+
#[Override] public function i(): void {} // ERROR i
97+
}
98+
99+
class C extends P implements I {}
100+
}
101+
102+
103+
104+
namespace overrideRfcExample9 {
105+
106+
use DaveLiddament\PhpLanguageExtensions\Override;
107+
108+
class P {
109+
private function p(): void {}
110+
}
111+
112+
class C extends P {
113+
#[Override] public function p(): void {} // ERROR p
114+
}
115+
}
116+

src/Override.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DaveLiddament\PhpLanguageExtensions;
6+
7+
/**
8+
* Add to a method to indicate it is overriding a method in a parent class/interface.
9+
*/
10+
#[\Attribute(\Attribute::TARGET_METHOD)]
11+
final class Override
12+
{
13+
public function __construct()
14+
{
15+
}
16+
}

0 commit comments

Comments
 (0)