php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64079 Trait with optional parameters cannot implement Interface
Submitted: 2013-01-26 00:04 UTC Modified: 2015-06-05 13:31 UTC
Votes:6
Avg. Score:4.3 ± 0.9
Reproduced:6 of 6 (100.0%)
Same Version:2 (33.3%)
Same OS:0 (0.0%)
From: php dot net-bug at spamhereplease dot com Assigned: cmb (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.4.11 OS: *
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: php dot net-bug at spamhereplease dot com
New email:
PHP Version: OS:

 

 [2013-01-26 00:04 UTC] 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])


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-06-05 13:31 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2015-06-05 13:31 UTC] cmb@php.net
This issue has been fixed as of PHP 5.4.14, see <http://3v4l.org/42bbB>.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 01:01:35 2025 UTC