php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63177 Implementing interface in sub-class with a trait triggers fatal error
Submitted: 2012-09-28 11:33 UTC Modified: 2017-12-23 19:52 UTC
Votes:3
Avg. Score:4.7 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:0 (0.0%)
From: maciej dot sz at gmail dot com Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.4Git-2012-09-28 (snap) 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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: maciej dot sz at gmail dot com
New email:
PHP Version: OS:

 

 [2012-09-28 11:33 UTC] maciej dot sz at gmail dot com
Description:
------------
This happens when method of a sub-class introduce new, optional parameters which are needed for an interface implementation. If a trait is source for the implementation of that method then a fatal error is triggered:
 Fatal error: Declaration of Base::push() must be compatible with I::push

Bug #60153 might be related as it considers opposite situation.

I've checked this with:
5.4.1
5.4.7
5.5-dev (snap 201209280930)

Test script:
---------------
<?php
error_reporting(E_ALL);

interface I
{
    public function push($val, $scope = null);
}

trait T
{
    public function push($val, $scope = null){}
}

class Base
{
    public function push($val){}
}

class Sub extends Base
{
    use T;
}

$SubReflection = new ReflectionClass('Sub');
// this shows correct push() method, compatible with 'I' interface:
echo $SubReflection->getMethod('push');

// however adding 'implements I' triggers fatal error:
class SubImplements extends Base implements I
{
    use T;
}

Expected result:
----------------
No errors (interface is implemented properly)

Actual result:
--------------
Fatal error: Declaration of Base::push() must be compatible with I::push($val, $scope = NULL) 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-09-28 12:09 UTC] maciej dot sz at gmail dot com
In case if someone has the same problem I came up with an ugly workaround, which requires yet another sub class:

<?php
class Sub extends Base
{
    use T;
}

class SubSub extends Sub implements I
{}
 [2016-09-22 20:06 UTC] rjhdby@php.net
Automatic comment from SVN on behalf of rjhdby
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=340111
Log: Translated by anonymous #63177
 [2017-12-23 19:52 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2017-12-23 19:52 UTC] nikic@php.net
This has been fixed as part of bug #71414 in PHP 7.0.6.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 14:01:29 2024 UTC