Skip to content

Commit

Permalink
Merge pull request #22 from DaveLiddament/feature/override
Browse files Browse the repository at this point in the history
ADD Override attribute
  • Loading branch information
DaveLiddament authored Sep 15, 2023
2 parents c6f59c6 + 0d96774 commit 11042ad
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The intention, at least initially, is that these extra language features are enf
- [Friend](#friend)
- [NamespaceVisibility](#namespaceVisibility)
- [InjectableVersion](#injectableVersion)
- [Override](#override)
- [Sealed](#sealed)
- [TestTag](#testtag)

Expand All @@ -30,6 +31,7 @@ The intention, at least initially, is that these extra language features are enf
- [Friend](#friend)
- [NamespaceVisibility](#namespaceVisibility)
- [InjectableVersion](#injectableVersion)
- [Override](#override)
- [Sealed](#sealed)
- [TestTag](#testtag)
- Deprecated
Expand Down Expand Up @@ -369,8 +371,16 @@ class MyService

```

## Override

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.

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.

NOTE:

- If you are using PHP 8.3 then use the real `#[Override]` attribute.
- This implementation doesn't consider traits.

## Sealed

Expand Down
51 changes: 51 additions & 0 deletions examples/override/overrideOnClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace OverrideOnClass {


use DaveLiddament\PhpLanguageExtensions\Override;

abstract class BaseClass {

abstract public function method1(): void;

public function method2(): void {}

public function anotherMethod(): void {}

}


class Class1 extends BaseClass {

#[Override] public function method1(): void
{
}

#[Override] public function method2(): void
{
}

#[Override] public function method4(): void // ERROR
{
}
}


new class extends BaseClass {
#[Override] public function method1(): void
{
}

#[Override] public function method2(): void
{
}

#[Override] public function method3(): void // ERROR
{
}
};

}
51 changes: 51 additions & 0 deletions examples/override/overrideOnInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace OverrideOnInterface {


use DaveLiddament\PhpLanguageExtensions\Override;

abstract class AnInterface {

abstract public function method1(): void;

public function method2(): void {}

public function anotherMethod(): void {}

}


class Class1 extends AnInterface {

#[Override] public function method1(): void
{
}

#[Override] public function method2(): void
{
}

#[Override] public function method4(): void // ERROR method4
{
}
}


new class extends AnInterface {
#[Override] public function method1(): void
{
}

#[Override] public function method2(): void
{
}

#[Override] public function method3(): void // ERROR method3
{
}
};

}
116 changes: 116 additions & 0 deletions examples/override/overrideRfcExamples.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

declare(strict_types=1);

namespace overrideRfcExample1 {


use DaveLiddament\PhpLanguageExtensions\Override;

class P {
protected function p(): void {}
}

class C extends P {
#[Override]
public function p(): void {}
}
}

namespace overrideRfcExample2 {

use DaveLiddament\PhpLanguageExtensions\Override;

class Foo implements \IteratorAggregate
{
#[Override]
public function getIterator(): \Traversable
{
yield from [];
}
}
}


namespace overrideRfcExample5 {

use DaveLiddament\PhpLanguageExtensions\Override;

interface I {
public function i();
}

interface II extends I {
#[Override]
public function i();
}

class P {
public function p1() {}
public function p2() {}
public function p3() {}
public function p4() {}
}

class PP extends P {
#[Override]
public function p1() {}
public function p2() {}
#[Override]
public function p3() {}
}

class C extends PP implements I {
#[Override]
public function i() {}
#[Override]
public function p1() {}
#[Override]
public function p2() {}
public function p3() {}
#[Override]
public function p4() {}
public function c() {}
}
}

namespace overrideRfcExample6 {

use DaveLiddament\PhpLanguageExtensions\Override;

class C
{
#[Override] public function c(): void {} // ERROR c
}
}

namespace overrideRfcExample7 {

use DaveLiddament\PhpLanguageExtensions\Override;

interface I {
public function i(): void;
}

class P {
#[Override] public function i(): void {} // ERROR i
}

class C extends P implements I {}
}



namespace overrideRfcExample9 {

use DaveLiddament\PhpLanguageExtensions\Override;

class P {
private function p(): void {}
}

class C extends P {
#[Override] public function p(): void {} // ERROR p
}
}

16 changes: 16 additions & 0 deletions src/Override.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace DaveLiddament\PhpLanguageExtensions;

/**
* Add to a method to indicate it is overriding a method in a parent class/interface.
*/
#[\Attribute(\Attribute::TARGET_METHOD)]
final class Override
{
public function __construct()
{
}
}

0 comments on commit 11042ad

Please sign in to comment.