php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46594 call to OtherClass::__construct within MyClass::__construct
Submitted: 2008-11-17 14:04 UTC Modified: 2008-11-21 16:06 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: dckorah at gmail dot com Assigned:
Status: Closed Package: Class/Object related
PHP Version: 5.2.6 OS: Linux (Ububtu 7.10)
Private report: No CVE-ID: None
 [2008-11-17 14:04 UTC] dckorah at gmail dot com
Description:
------------
<?php
class A {
    function __construct() {
        $this->a_property = 'property set from A';
    }
}

class B {
    function __construct() {
        A::__construct();
        $this->b_property = 'property set from B';
    }
}
?>
In the above code, if you initialise class B and inspect the properties of it, you will see that both a_property and b_property are set.

This is quite unexpected from an OO language.


version info:
PHP 5.2.3-1ubuntu6.4 (cli) (built: Jul 23 2008 06:20:35)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies


Reproduce code:
---------------
<?php

class A {
    function __construct() {
        $this->a_property = 'property set from A';
    }
}

class B {
    function __construct() {
        A::__construct();
        $this->b_property = 'property set from B';
    }
}

?>


Expected result:
----------------
Class A has got nothing to do with class B and this should not happen. If at all an user trys this, it should throw an error.

Actual result:
--------------
Please see description.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-18 23:29 UTC] jani@php.net
Actually this has nothing to do with __construct(), same happens with 
"regular" methods too.
 [2008-11-21 16:06 UTC] jani@php.net
This is actually fixed in PHP_5_3 branch already:

[jani@localhost ~]$ src/build/php_5_3/sapi/cli/php -n t.php

Fatal error: Non-static method A::__construct() cannot be called statically, assuming $this from incompatible context in /home/jani/t.php on line 11
[jani@localhost ~]$ src/build/php_5_2/sapi/cli/php -n t.php
object(B)#1 (2) {
  ["a_property"]=>
  string(19) "property set from A"
  ["b_property"]=>
  string(19) "property set from B"
}

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 21:01:33 2024 UTC