rfc:allow-abstract-function-override
PHP RFC: Allow abstract function overrideVersion: 0.1
Date: 2017-03-01
Author: @WesNetmo @ Twitter
Status: Implemented (in PHP 7.2)
PHP 7 introduced type declarations for return and improved ones for parameters. They currently support a very limited kind of variance (only to no-type):
<?php class A { function bar(stdClass $x){} } class B extends A { function bar($x): stdClass{} }
However, this isn't currently matched by the equivalent abstract declarations:
<?php abstract class A { abstract function bar(stdClass $x); } abstract class B extends A { abstract function bar($x): stdClass; } class C extends B { function bar($x): stdClass{} } // Fatal error: Can't inherit abstract function A::bar() // (previously declared abstract in B)
This RFC proposes to allow this, even if it has very few uses, and because there is no reason to disallow a compatible redefinition.
Additionally, it comes in handy for documentation:
interface A{ function doSomething(); } interface B extends A{ function doSomethingElse(); } abstract class AProxy implements A{ abstract protected function getOrigin(): A; function doSomething(){ return $this->getOrigin()->doSomething(); } } // This phpdoc syntax has quirks in phpdocumentor // apigen and phpstorm, and it's almost unmanageable // as phpdoc interpreters have poor multiline support // (imagine documenting all the parameters, the return // type, all the throws...): /** @method B getOrigin() */ abstract class BProxy extends AProxy implements B{ /** @return B */ // This is much better! abstract protected function getOrigin(): A; function doSomethingElse(){ return $this->getOrigin()->doSomethingElse(); } }
And, obviously, it will gain more uses when PHP will support a full-featured type variance.
Backward Incompatible ChangesNone.
Proposed PHP Version(s)7.2
Voting2/3 majority required.
Patches and TestsPull request to handle the change: https://github.com/php/php-src/pull/2531
Referencesrfc/allow-abstract-function-override.txt ยท Last modified: 2025/04/03 13:08 by 127.0.0.1
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.3