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: -
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: Open Package: *General Issues
PHP Version: 5.3.18 OS: Irrelevant
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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

Add a Patch

Pull Requests

Add a Pull Request

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.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Mon Sep 16 04:01:26 2019 UTC