|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2011-07-03 20:14 UTC] cataphract@php.net
-Status: Open
+Status: Bogus
[2011-07-03 20:14 UTC] cataphract@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Dec 16 23:00:01 2025 UTC |
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: