From:             
Operating system: Windows
PHP version:      5.3SVN-2011-07-03 (SVN)
Package:          Unknown/Other Function
Bug Type:         Bug
Bug description:Irrelevant behavior calling parent::__construct(..) in child 
class constructor 

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 bug report at https://bugs.php.net/bug.php?id=55117&edit=1
-- 
Try a snapshot (PHP 5.2):            
https://bugs.php.net/fix.php?id=55117&r=trysnapshot52
Try a snapshot (PHP 5.3):            
https://bugs.php.net/fix.php?id=55117&r=trysnapshot53
Try a snapshot (trunk):              
https://bugs.php.net/fix.php?id=55117&r=trysnapshottrunk
Fixed in SVN:                        
https://bugs.php.net/fix.php?id=55117&r=fixed
Fixed in SVN and need be documented: 
https://bugs.php.net/fix.php?id=55117&r=needdocs
Fixed in release:                    
https://bugs.php.net/fix.php?id=55117&r=alreadyfixed
Need backtrace:                      
https://bugs.php.net/fix.php?id=55117&r=needtrace
Need Reproduce Script:               
https://bugs.php.net/fix.php?id=55117&r=needscript
Try newer version:                   
https://bugs.php.net/fix.php?id=55117&r=oldversion
Not developer issue:                 
https://bugs.php.net/fix.php?id=55117&r=support
Expected behavior:                   
https://bugs.php.net/fix.php?id=55117&r=notwrong
Not enough info:                     
https://bugs.php.net/fix.php?id=55117&r=notenoughinfo
Submitted twice:                     
https://bugs.php.net/fix.php?id=55117&r=submittedtwice
register_globals:                    
https://bugs.php.net/fix.php?id=55117&r=globals
PHP 4 support discontinued:          
https://bugs.php.net/fix.php?id=55117&r=php4
Daylight Savings:                    https://bugs.php.net/fix.php?id=55117&r=dst
IIS Stability:                       
https://bugs.php.net/fix.php?id=55117&r=isapi
Install GNU Sed:                     
https://bugs.php.net/fix.php?id=55117&r=gnused
Floating point limitations:          
https://bugs.php.net/fix.php?id=55117&r=float
No Zend Extensions:                  
https://bugs.php.net/fix.php?id=55117&r=nozend
MySQL Configuration Error:           
https://bugs.php.net/fix.php?id=55117&r=mysqlcfg
Try a snapshot (PHP 5.4):            
https://bugs.php.net/fix.php?id=55117&r=trysnapshot54

Reply via email to