Bug #63177 [Opn->Csd]: Implementing interface in sub-class with a trait triggers fatal error

From: Date: Sat, 23 Dec 2017 19:52:10 +0000
Subject: Bug #63177 [Opn->Csd]: Implementing interface in sub-class with a trait triggers fatal error
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-213261@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=63177&edit=1

 ID:                 63177
 Updated by:         nikic@php.net
 Reported by:        maciej dot sz at gmail dot com
 Summary:            Implementing interface in sub-class with a trait
                     triggers fatal error
-Status:             Open
+Status:             Closed
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   irrelevant
 PHP Version:        5.4Git-2012-09-28 (snap)
-Assigned To:        
+Assigned To:        nikic
 Block user comment: N
 Private report:     N

 New Comment:

This has been fixed as part of bug #71414 in PHP 7.0.6.


Previous Comments:
------------------------------------------------------------------------
[2016-09-22 20:06:11] rjhdby@php.net

Automatic comment from SVN on behalf of rjhdby
Revision: http://svn.php.net/viewvc/?view=revision&revision=340111
Log: Translated by anonymous #63177

------------------------------------------------------------------------
[2012-09-28 12:09:06] 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
{}

------------------------------------------------------------------------
[2012-09-28 11:33:31] 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) 


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=63177&edit=1


Thread (3 messages)

« previous php.bugs (#213261) next »