php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63371 Overriding method with an abstract method
Submitted: 2012-10-26 17:16 UTC Modified: 2021-08-06 16:03 UTC
Votes:8
Avg. Score:4.2 ± 0.8
Reproduced:8 of 8 (100.0%)
Same Version:0 (0.0%)
Same OS:2 (25.0%)
From: dagguh at gmail dot com Assigned:
Status: Suspended Package: *General Issues
PHP Version: 5.3.18 OS: Irrelevant
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dagguh at gmail dot com
New email:
PHP Version: OS:

 

 [2012-10-26 17:16 UTC] dagguh at gmail dot com
Description:
------------
In Java you can override a method with an abstract one, but in PHP it results in:
Fatal error: Cannot make non abstract method 
IdentifiableExceptionTests::getSystemUnderTest() abstract in class 
RestResponseExceptionTests in xxx on line yyy


Test script:
---------------
class HttpNotFoundException extends RestResponseException {
    
    public function getHttpStatusCode() {
        // ...
    }
}

abstract class RestResponseException extends IdentifiableException {

    public abstract function getHttpStatusCode();
}

class IdentifiableException extends Exception {

    // ...
}

// ----------------------- and here come the tests:

class HttpNotFoundExceptionTests extends RestResponseExceptionTests {

    protected function getSystemUnderTest() {
        return new HttpNotFoundException();
    }

    // ... tests logic specific to HttpNotFoundException

}

class RestResponseExceptionTests extends IdentifiableExceptionTests {

    /**
     * @return RestResponseException
     */
    protected abstract function getSystemUnderTest();

    // ... tests specific for RestResponseException, even though it is an abstract class, it has some logic implemented and tests for that portion of logic are specified here. It is important to note that while getSystemUnderTest()  method in IdentifiableExceptionTests expects IdentifiableException, in RestResponseExceptionTests it expects RestResponseException, which is a proper subclass of IdentifiableException

}

class IdentifiableExceptionTests extends PHPUnit_Framework_TestCase {

    /**
     * @return IdentifiableException
     */
    protected function getSystemUnderTest() {
        return new IdentifiableException();
    }

    //... tests IdentifiableException logic
}


Expected result:
----------------
I expect this code to work.
It works in Java, so why cannot it work in PHP?

Actual result:
--------------
Fatal error: Cannot make non abstract method 
IdentifiableExceptionTests::getSystemUnderTest() abstract in class 
RestResponseExceptionTests in xxx on line yyy

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-10-26 17:23 UTC] hanskrentel at yahoo dot de
> It works in Java, so why cannot it work in PHP?

Because PHP is PHP and Java is Java. So where to file the bug-report? Shouldn't it 
be reported as a bug at Java, because Java doesn't do it like PHP?
 [2012-10-26 18:10 UTC] dagguh at gmail dot com
@hanskrentel:
I made the comparison, because Java is generally more restrictive than PHP.
This time, it is PHP which imposes a restriction, which is not present in Java. 
Furthermore, this restriction is aribtrary and completely unnecessary.

Moreover, PHP is strongly influenced from Java since PHP5.
 [2021-08-06 16:03 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2021-08-06 16:03 UTC] cmb@php.net
If you or anybody else is still interested, I suggest to pursue
the RFC process[1].  For the time being, I suspend this ticket.

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 11:01:30 2024 UTC