php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73220 Classes implementing an interface can violate its method's signatures
Submitted: 2016-10-01 19:53 UTC Modified: 2016-10-06 16:10 UTC
From: phansys at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.0.11 OS: any
Private report: No CVE-ID: None
 [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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-10-01 21:50 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 [2016-10-01 21:50 UTC] rasmus@php.net
Adding additional default values does not violate LSP (Liskov Substitution Principle) in that in your example, B can be used in place of any other class that implements A and as such it is entirely compatible with A and there should be no error.
 [2016-10-02 06:12 UTC] bradyn at bradynpoulsen dot com
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.
 [2016-10-02 06:16 UTC] requinix@php.net
-Package: PHP Language Specification +Package: Scripting Engine problem
 [2016-10-02 13:16 UTC] phansys at gmail dot com
So, should I create another bug report for Object Interfaces documentation? 
http://php.net/manual/en/language.oop5.interfaces.php#language.oop5.interfaces

It states in a note:

"The class implementing the interface must use the exact same method signatures as are defined in the interface. Not doing so will result in a fatal error."

Which doesn't seem to mention LS principle in any way.
 [2016-10-04 00:49 UTC] phansys at gmail dot com
-Package: Scripting Engine problem +Package: Documentation problem
 [2016-10-04 00:49 UTC] phansys at gmail dot com
http://php.net/manual/en/language.oop5.interfaces.php#language.oop5.interfaces states in a note:

"The class implementing the interface must use the exact same method signatures as are defined in the interface. Not doing so will result in a fatal error."

Which doesn't seem to mention LS principle in any way.
 [2016-10-06 16:10 UTC] requinix@php.net
-Package: Documentation problem +Package: Scripting Engine problem
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 01 04:01:30 2024 UTC