php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #63832 Doc don't fully cover constructor inheritance
Submitted: 2012-12-21 21:03 UTC Modified: 2013-01-16 15:54 UTC
From: jschoett at gmail dot com Assigned: googleguy (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.4.10 OS: Doc problem
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jschoett at gmail dot com
New email:
PHP Version: OS:

 

 [2012-12-21 21:03 UTC] jschoett at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/language.oop5.decon
---
I can not find a definite statement on this:
"The child class inherits the constructor of the base class, iff the child class 
does not explicitly define an own constructor."

The documentation do not cover this with:
"Note: Parent constructors are not called implicitly if the child class defines a 
constructor"

In addition, since this is an significant difference to e.g. Java, it should be 
documented

Test script:
---------------
Snippet to validate this statement (I know you already know the result):
<?php
class A {
    private $x = null;
    function __construct($x) {
        $this->x = $x;
    }
}
class B extends A { }
$b1 = new B(); // Warning: Missing argument 1 for A::__construct()
$b2 = new B(1);
var_dump($b1); // Notice: Undefined variable: x
var_dump($b2);
?>

Expected result:
----------------
Same as actual, but I wan't it to be documented :)

Actual result:
--------------
Warning: Missing argument 1 for A::__construct(), called in path\testoutput.php on 
line 28 and defined in path\testoutput.php on line 21

Notice: Undefined variable: x in path\testoutput.php on line 22
object(B)#1 (1) { ["x":"A":private]=> NULL } object(B)#2 (1) { ["x":"A":private]=> 
int(1) }

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-27 09:17 UTC] jschoett at gmail dot com
just title adapted.
 [2012-12-27 09:17 UTC] jschoett at gmail dot com
-Summary: Doc don't fully cover constr. inheritance +Summary: Doc don't fully cover constructor inheritance
 [2013-01-01 00:08 UTC] mail+php at requinix dot net
It's not so much that the child would inherit the constructor as it would 
inherit the parent's "__construct" method (if it's public or protected). There's 
a subtle difference there: the constructor is not ipso facto special that way, 
it's just that PHP looks for a method with that name.

Considering the importance of a constructor and how it's used I think it's worth 
mentioning that it obeys the normal method inheritance rules.

Did you know you can submit changes to the online documentation? Look for the 
edit link in the top-right corner of manual pages.

"oop5.decon: spelling out that ctors and dtors can be inherited (bug #63832)"
http://edit.php.net/?patchID=796&project=PHP
 [2013-01-16 15:17 UTC] googleguy@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.


 [2013-01-16 15:17 UTC] googleguy@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: googleguy
 [2013-01-16 15:54 UTC] jschoett at gmail dot com
Thank you for fixing this issue!
 [2013-01-16 19:18 UTC] googleguy@php.net
Automatic comment from SVN on behalf of googleguy
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=329168
Log: Fxies bug #63832
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 14:00:02 2026 UTC