php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55117 Irrelevant behavior calling parent::__construct(..) in child class constructor
Submitted: 2011-07-03 10:30 UTC Modified: 2011-07-03 20:14 UTC
From: singams dot teja at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.3SVN-2011-07-03 (SVN) OS: Windows
Private report: No CVE-ID: None
 [2011-07-03 10:30 UTC] singams dot teja at gmail dot com
Description:
------------
parent::__construct() behaves weirdly when it is not used as first statement in the child class constructor. This is observed when we use PHP version 5.3.5. 

When we try to create an object of the subclass with "parent::__construct(..)" not as the first statement, ideally PHP should through an error intact with the OO concepts implemented in most of other languages (Java,etc). But to our astonishment it just executes the statement as a function call and it prints the statements from the parent class constructor but the changes are not persisted with the current object. 

The observed result was that the default constructor instantiates the object of baseclass if the parent::__construct(..) is not present as first statement. This implies that there is no use to have parent::__construct(..) in subclass constructor when called in place other than as first statement. In such case, the PHP runtime should be intelligent enough to either report it as error or skip that step during the execution. This could be a potential bug where it might be misleading the coder to fall into trap of dynamic construction/updation of content of base class object.

Possible Solution:
-----------------
PHP runtime can set a flag with $this which indicates the completion of creation of object and hence any further calls parent::__construct(..) will not be entertained based on the flag status being set.

Thanks,
Loknath Priyatham Teja Singamsetty.
Gurram Shekar.


Test script:
---------------
class BaseClass {
   protected $test;
   function __construct($abc) {
       print "In BaseClass constructor\n";
       $test = $abc;
       echo $test;
   }
}

class SubClass extends BaseClass {
   function __construct($abc) {
     print "In SubClass constructor"; 
     parent::__construct($abc);
     echo "Test Value:".$this->test;
   }
}

$obj = new SubClass("345"); 

Expected result:
----------------
In SubClass constructor
Test Value:

P.S: (Assuming that when the default constructor is called and the parent::__construct(..) is present at places other than as first statement should be omitted)

Actual result:
--------------
In SubClass constructor
In BaseClass constructor
345
Test Value:

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-07-03 20:14 UTC] cataphract@php.net
-Status: Open +Status: Bogus
 [2011-07-03 20:14 UTC] cataphract@php.net
BaseClass::__construct has a bug; should be "$this->test = ...", not "$test", otherwise the changes would be "persisted with the current object", as you put it.

Other than that, PHP's behavior with constructor is very idiosyncratic and has the practical consequence that (non final) constructors cannot be used to enforce invariants in subclasses. In any case, no bug here.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 18 17:00:03 2025 UTC