Take the State of PHP 2026 Survey →

Allow abstract function override View as Markdown

Implemented (PHP 7.2)
Authors
@WesNetmo @ Twitter
Date
2017-03-01
Version
0.1
Source
wiki.php.net/rfc/allow-abstract-function-override

Vote

Accept abstract function override? closed
Yes
16
No
3
19 voters
ashnazg No 2017-03-29 13:44
bwoebi Yes 2017-03-28 15:51
cyberline Yes 2017-04-07 17:55
daverandom Yes 2017-04-03 12:23
dm Yes 2017-03-28 17:17
emir Yes 2017-03-28 17:55
galvao Yes 2017-04-08 03:32
kelunik Yes 2017-03-29 08:32
kguest Yes 2017-03-28 20:19
marcio Yes 2017-03-30 18:30
mariano No 2017-03-28 13:27
ocramius Yes 2017-03-28 16:41
patrickallaert No 2017-04-06 08:26
salathe Yes 2017-03-30 12:41
sammyk Yes 2017-03-31 17:01
stas Yes 2017-04-10 19:46
subjective Yes 2017-04-07 17:54
treffynnon Yes 2017-03-29 06:43
trowski Yes 2017-03-28 16:10

Discussions on the mailing list

[RFC][VOTE] Allow abstract function override
· 15 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Proposal

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 Changes

None.

Proposed PHP Version(s)

7.2

Voting

2/3 majority required.

Vote: Accept abstract function override? — Yes: 16 · No: 3 (closed)

Patches and Tests

Pull request to handle the change: https://github.com/php/php-src/pull/2531

References

Discussion on externals.io