|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2016-10-01 19:53 UTC] phansys at gmail dot com
Description:
------------
Classes implementing an interface can violate its method's signatures by adding more arguments with a default value.
Test script:
---------------
interface A
{
public function test($one = null);
}
class B implements A
{
public function test($one = null, $two = null)
{
var_dump($one, $two);
}
}
Expected result:
----------------
Fatal error: Declaration of B::test($one, $two) must be compatible with A::test($one = NULL)
Actual result:
--------------
No error.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 20:00:01 2025 UTC |
Adding another optional parameter is totally allowed by LSP. As long as the implementing class allows you to call `test()` or `test('someValue')` as specified by the interface, the class is honoring the method signature. However, if class B were defined as: class B implements A { public function test($one = null, $two) { var_dump($one, $two); } } it would then be violating the signature in requiring the 2nd parameter to be passed. As such, you would receive the expected result mentioned above.