php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74722 Overridden method with same name as child class
Submitted: 2017-06-09 01:35 UTC Modified: 2017-06-09 11:20 UTC
From: mb at iname dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.1.6 OS: CentOS 6
Private report: No CVE-ID: None
 [2017-06-09 01:35 UTC] mb at iname dot com
Description:
------------
The example below produces a fatal error:
Fatal error: Call to protected b::b() from invalid context in test.php on line 17

Changing visibility of child's method to public results in b::b() being invoked twice (prints 'ChildChild').

Renaming the child class to anything but 'b' resolves the issue.

Test script:
---------------
<?php
class a {
  protected function b() { echo 'Parent'; }
  public function test() {
    $this->b();
  }
}
class b extends a {
  protected function b() { echo 'Child'; }
}
$o = new b();
$o->test();

Expected result:
----------------
The script should print "Child".

Alternatively, if methods with same name as class are not allowed, the script should fail at parsing stage.

Actual result:
--------------
Fatal error: Call to protected b::b() from invalid context in test.php on line 17

If b::b() is set public in child, prints 'ChildChild'


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-06-09 01:41 UTC] mail at pmmaga dot net
What you are seeing is the result of your method b() in class b being used as a constructor for it. See: http://php.net/manual/en/language.oop5.decon.php for more info
 [2017-06-09 01:44 UTC] daverandom@php.net
-Status: Open +Status: Not a bug
 [2017-06-09 01:44 UTC] daverandom@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

This is only an issue in the case of methods that have the same name as the class, which is treated as a PHP4 (Java-style) constructor.

The following works as expected:

https://3v4l.org/5CHep

See also:

https://wiki.php.net/rfc/remove_php4_constructors
 [2017-06-09 11:12 UTC] mb at iname dot com
Thank you, I forgot about PHP4 constructors. However, shouldn't PHP emit E_DEPRECATED notice in this case? Documentation states it should, but it doesn't for me, even if I call error_reporting(E_ALL | E_DEPRECATED);
 [2017-06-09 11:20 UTC] requinix@php.net
It does give the deprecated warning. https://3v4l.org/WT1r7

PHP discovers the old-style constructors when it first analyzes the class, and that happens when the file is loaded. No code in the file has been executed at that point so calling error_reporting() in it will not make a difference (for that warning).
Best thing is to find your php.ini and change the error_reporting setting.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 02:01:29 2024 UTC