From: php dot net-bug at spamhereplease dot com
Operating system: *
PHP version: 5.4.11
Package: Scripting Engine problem
Bug Type: Bug
Bug description:Trait with optional parameters cannot implement Interface
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 bug report at https://bugs.php.net/bug.php?id=64079&edit=1
--
Try a snapshot (PHP 5.4):
https://bugs.php.net/fix.php?id=64079&r=trysnapshot54
Try a snapshot (PHP 5.3):
https://bugs.php.net/fix.php?id=64079&r=trysnapshot53
Try a snapshot (trunk):
https://bugs.php.net/fix.php?id=64079&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=64079&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=64079&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=64079&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=64079&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=64079&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=64079&r=support
Expected behavior: https://bugs.php.net/fix.php?id=64079&r=notwrong
Not enough info:
https://bugs.php.net/fix.php?id=64079&r=notenoughinfo
Submitted twice:
https://bugs.php.net/fix.php?id=64079&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=64079&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=64079&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=64079&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=64079&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=64079&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=64079&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=64079&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=64079&r=mysqlcfg