php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27367 A protected parent constructor cannot be called
Submitted: 2004-02-23 12:12 UTC Modified: 2004-02-23 12:21 UTC
From: andrey@php.net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5CVS-2004-02-23 (dev) OS: linux
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: andrey@php.net
New email:
PHP Version: OS:

 

 [2004-02-23 12:12 UTC] andrey@php.net
Description:
------------
By default if a constructor in a subclass is not defined the constructor of the parent class is being called (see example 1).
However, if the parent constructor is declared "protected" and is visible to the child, the engine refuses to execute the constructor (Example 2 and result 2).
Example 3 shows that the parent constructor is visible in the child when an explicit constructor is provided there.

Reproduce code:
---------------
Ex. 1:
php -r 'class a { function a() {echo "const of a\n";}} class b extends a {} $a = new b();'

Ex. 2:
php -r 'class a { protected function a() {echo "const of a\n";}} class b extends a {} $a = new b();'

Ex. 3:
php -r 'class a { protected function a() {echo "const of a\n";}} class b extends a {public function b(){parent::a();}} $a = new b();'


Expected result:
----------------
const of a
const of a
const of a

Actual result:
--------------
Result 1 :
const of a

Result 2 :
PHP Fatal error:  Call to protected a::a() from context '' in Command line code on line 1

Result 3 :
const of a

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-02-23 12:21 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

protected or private constructors are callable from static members aka factory pattern.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Sep 10 12:00:02 2025 UTC