php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60217 imposing requirements in traits
Submitted: 2011-11-04 13:12 UTC Modified: 2011-11-05 01:48 UTC
From: maus dot christian at googlemail dot com Assigned: gron (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.4.0beta2 OS: Ubuntu Linux 11.10
Private report: No CVE-ID: None
 [2011-11-04 13:12 UTC] maus dot christian at googlemail dot com
Description:
------------
Using multiple traits that depend on the same requirement using an abstract method fails with a fatal error.
I tried using trait composition in order to move the requirement into a common trait, but this does not work either.



Test script:
---------------
trait DoubleAmount {
    public function doubleAmount() {
        return $this->getAmount() * 2;
    }
    public abstract function getAmount();
}
trait TripleAmount {
    public function tripleAmount() {
        return $this->getAmount() * 3;
    }
    public abstract function getAmount();
}
class model {
    use DoubleAmount, TripleAmount;

    public function getAmount() {
        return 23;
    }
}

Actual result:
--------------
PHP Fatal error:  Can't inherit abstract function TripleAmount::getAmount() (previously declared abstract in DoubleAmount) in /home/maus/projects/traitstest/trait_abstract.php on line 30

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-11-05 00:10 UTC] gron@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: gron
 [2011-11-05 01:46 UTC] gron@php.net
Automatic comment from SVN on behalf of gron
Revision: http://svn.php.net/viewvc/?view=revision&revision=318793
Log: Fixed Bug #60217 (Requiring the same method from different traits)
- also added test to check for inconsistent abstract method definitions, they need to be compatible
 [2011-11-05 01:48 UTC] gron@php.net
-Status: Assigned +Status: Closed
 [2011-11-05 01:48 UTC] gron@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.

Fixed with SVN rev. 318793.

I also added the missing tests that ensure compatibility of abstract method 
definitions.
 [2012-04-18 09:47 UTC] laruence@php.net
Automatic comment on behalf of gron
Revision: http://git.php.net/?p=php-src.git;a=commit;h=12cf1b797824e1e0ce260fed23d9e7ae853c9aad
Log: Fixed Bug #60217 (Requiring the same method from different traits) - also added test to check for inconsistent abstract method definitions, they need to be compatible
 [2012-07-24 23:39 UTC] rasmus@php.net
Automatic comment on behalf of gron
Revision: http://git.php.net/?p=php-src.git;a=commit;h=12cf1b797824e1e0ce260fed23d9e7ae853c9aad
Log: Fixed Bug #60217 (Requiring the same method from different traits) - also added test to check for inconsistent abstract method definitions, they need to be compatible
 [2013-11-17 09:35 UTC] laruence@php.net
Automatic comment on behalf of gron
Revision: http://git.php.net/?p=php-src.git;a=commit;h=12cf1b797824e1e0ce260fed23d9e7ae853c9aad
Log: Fixed Bug #60217 (Requiring the same method from different traits) - also added test to check for inconsistent abstract method definitions, they need to be compatible
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 11:01:28 2024 UTC