php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #41090 can't override inherited private methods
Submitted: 2007-04-15 01:12 UTC Modified: 2007-04-27 14:42 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: ozone at cname dot com Assigned: colder (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.2.1 OS: linux
Private report: No CVE-ID: None
 [2007-04-15 01:12 UTC] ozone at cname dot com
Description:
------------
The page on Visibility states: "Private limits visibility only to the class that defines the item." Apparently, private methods may not be superseded by a child of that class; in the following code, a new object e inherits the __constructor() which calls "$this->df", but because f() is declared private, it is silently not overridden. This behavior may not constitute a "bug" in the context of PHP inheritance, but it deserves a warning message and/or some mention in the documentation.

Note that if f() is declared protected (or public) in both classes, inheritance works as expected; if the two f()s are declared with differing protection, an error message results, which is somewhat ironic considering the above-described silent failure mode.


Reproduce code:
---------------
class d {
 function __construct() {
  $this->f();
 }
 private function f() {
  echo "d->f()\n";
 }
}
class e extends d {
 private function f() {
  echo "e->f()\n";
 }
}
$t = new e();


Expected result:
----------------
e->f()

(Because $this refers to an instance of e when it is executed.)

Actual result:
--------------
d->f()



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-04-15 09:04 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Calling scope matters
 [2007-04-15 19:20 UTC] ozone at cname dot com
This still needs better documentation; the (silent) inability to override a private method in a derivative class is somewhat counterintuitive. Put another way, I had to burn time writing test cases after a careful study of the documentation didn't mention the behavior I saw. IMNSHO, a production-quality language doesn't require careful "figuring out" of its behaviors, and if I'm repeatedly told that the issues I experience are "not a bug" , I'll stop wasting the time to file bug reports.
 [2007-04-27 14:42 UTC] colder@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

As an example.
 [2020-02-07 06:10 UTC] phpdocbot@php.net
Automatic comment on behalf of colder
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=2577e388f95c2ca9da66201639dc338513bede21
Log: Fix #41090 (overriding a private method) in an example
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 09 22:01:33 2025 UTC