php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72496 Cannot declare public method with signature incompatible with parent private me
Submitted: 2016-06-26 17:18 UTC Modified: 2016-07-05 12:39 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: postmaster at greg0ire dot fr Assigned: nikic (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.5.37 OS: Linux
Private report: No CVE-ID: None
 [2016-06-26 17:18 UTC] postmaster at greg0ire dot fr
Description:
------------
When declaring a public method in a child class, one can't reuse the name of a private method of the parent without being issued a Warning for php 7, a Strict Standards error for php 5.5 - 5.6, unless the signature is considered compatible.

Test script:
---------------
<?php

class Foo
{
    private function getName()
    {
        return 'John';
    }
}

class Bar extends Foo
{
    public function getName($extraArgument)
    {
        return $extraArgument;
    }
}

Demonstration : https://3v4l.org/SNlDO

Expected result:
----------------
The parent method is private : it can't be called from the outside and as far as the child class is concerned, it does not exist. Creating a public method with the same name is not overriding the method since methods in the parent class will still call the private method when asked to.

Actual result:
--------------
php 7.7 : Warning: Declaration of Bar::getName($extraArgument) should be compatible with Foo::getName() in /in/SNlDO on line 17

5.5.0 - 5.6.22 : Strict Standards: Declaration of Bar::getName() should be compatible with Foo::getName() in /in/SNlDO on line 17

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-28 11:23 UTC] nikic@php.net
-Status: Open +Status: Verified
 [2016-06-28 11:23 UTC] nikic@php.net
Right. LSP should not be enforced when overriding private methods.
 [2016-06-28 12:52 UTC] postmaster at greg0ire dot fr
> Right. LSP should not be enforced when overriding private methods.

Clearly. As a side note, this only happens if the method in the child class is public or protected.
 [2016-06-30 08:48 UTC] mail at pmmaga dot net
Added a pull request against 5.5. I also have the fix for master. Should I create a different PR for it?
 [2016-07-03 21:30 UTC] mail at pmmaga dot net
Added a second PR. This time, against 5.6
 [2016-07-05 12:39 UTC] nikic@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: nikic
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 09:01:30 2024 UTC