Bug #55117 [Opn->Bgs]: Irrelevant behavior calling parent::__construct(..) in child class constructor

From: Date: Mon, 04 Jul 2011 00:14:42 +0000
Subject: Bug #55117 [Opn->Bgs]: Irrelevant behavior calling parent::__construct(..) in child class constructor
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-161554@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=55117&edit=1

 ID:                 55117
 Updated by:         cataphract@php.net
 Reported by:        singams dot teja at gmail dot com
 Summary:            Irrelevant behavior calling parent::__construct(..)
                     in child class constructor
-Status:             Open
+Status:             Bogus
 Type:               Bug
 Package:            Unknown/Other Function
 Operating System:   Windows
 PHP Version:        5.3SVN-2011-07-03 (SVN)
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2011-07-03 10:30:50] 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:


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=55117&edit=1


Thread (2 messages)

« previous php.bugs (#161554) next »