Bug #64079 [Opn->Csd]: Trait with optional parameters cannot implement Interface

From: Date: Fri, 05 Jun 2015 13:31:49 +0000
Subject: Bug #64079 [Opn->Csd]: Trait with optional parameters cannot implement Interface
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-193143@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=64079&edit=1 ID: 64079 Updated by: cmb@php.net Reported by: php dot net-bug at spamhereplease dot com Summary: Trait with optional parameters cannot implement Interface -Status: Open +Status: Closed Type: Bug Package: Scripting Engine problem Operating System: * PHP Version: 5.4.11 -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: This issue has been fixed as of PHP 5.4.14, see <http://3v4l.org/42bbB>. Previous Comments: ------------------------------------------------------------------------ [2013-01-26 00:04:24] php dot net-bug at spamhereplease dot com Description: ------------ Implementing an interface via a trait doesn't appear to allow optional parameters like implementing via a class. That is, I can add optional parameters to the implementation method in a class and it still satisfies the interface. Adding optional parameters via a trait, however, ends as a fatal 'compatibility' error. It appears the expected behavior was working in at least 5.4.6, or (unlikely) that it works in a linux version but not a windows one. (Bug #63177 may be indirectly related.) Test script: --------------- //file testWorks.php interface IStringable { public function toString(); } class MyTestWorks implements IStringable { public function toString($a = '', $b = NULL) { echo "My Test is instantiated", PHP_EOL; } } $obj = new MyTestWorks(); $obj->toString(); //file testBroken.php interface IStringable { public function toString(); } trait TStringable { public function toString($a = '', $b = NULL) { echo "My Test is instantiated", PHP_EOL; } } class MyTestWorks implements IStringable{ use TStringable; } $obj = new MyTestWorks(); $obj->toString(); Expected result: ---------------- >php testWorks.php My Test is instantiated >php testBroken.php My Test is instantiated (This is the output from php 5.4.6 [linux]) Actual result: -------------- >php testWorks.php My Test is instantiated >php testBroken.php PHP Fatal error: Declaration of TStringable::toString($a = '', $b = NULL) must be compatible with IStringable::toString() in [...]\testBroken.php on line 17 (This is the output from php 5.4.11 [windows]) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=64079&edit=1

« previous php.bugs (#193143) next »