php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #55864 Explicit call to default constructor
Submitted: 2011-10-07 09:20 UTC Modified: 2020-12-18 13:36 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: dagguh at gmail dot com Assigned:
Status: Suspended Package: Class/Object related
PHP Version: 5.3.5 OS: Ubuntu 10.04.3 LTS
Private report: No CVE-ID: None
 [2011-10-07 09:20 UTC] dagguh at gmail dot com
Description:
------------
You cannot call default constructor from derived class.

Test script:
---------------
<?
class Base {

}

class Derived extends Base {

    function __construct($foo) {
        parent::__construct();
        echo $foo;
    }

}

$derived = new Derived("baz");

Expected result:
----------------
baz

Actual result:
--------------
Fatal error: Cannot call constructor

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-07 09:24 UTC] dagguh at gmail dot com
PS. Actually my PHP version is 5.3.5
 [2011-10-07 09:25 UTC] dagguh at gmail dot com
-Operating System: Irrelevant +Operating System: Ubuntu 10.04.3 LTS -PHP Version: 5.3SVN-2011-10-07 (SVN) +PHP Version: 5.3.5
 [2011-10-07 09:25 UTC] dagguh at gmail dot com
Fixed version and OS
 [2011-10-07 09:41 UTC] laruence@php.net
-Type: Bug +Type: Feature/Change Request
 [2011-10-07 09:41 UTC] laruence@php.net
Actually, there is no `default constructor` in PHP, you should decalare one be 
fore you call to it. 
I think this should be mark as a feature request.
 [2011-10-07 09:55 UTC] dagguh at gmail dot com
Well you can call:

$base = new Base();

which works, because php uses default, empty constructor.
If new operator can call it, why derived classes can't?
 [2011-10-07 10:03 UTC] laruence@php.net
yes, you can instantiate a calss without a constructor be declared, but that doesn't mean there is default empty constructor. 

in PHP, constructor most like a magic method which will be called after a object instantiated immediatlely. 
 
so if there is no constructor for a class, the new operator still can instantiate a class. but you can't call a no-exists constructor directly :)
 [2011-10-07 10:23 UTC] dagguh at gmail dot com
I stand by my request because of cohesion.
I often see useless code in derived classes, e.g.:

function __construct($name) {
    parent::__construct($name);
}

which does absolutely nothing and I delete this code, because php will do it 
automatically.
For the same reason I'd like to remove

function __construct() {
    ;
}

which also does absolutely nothing and php will do the same automatically EXCEPT 
in the case I described.

So, I stand by my request :)
 [2020-12-18 13:36 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2020-12-18 13:36 UTC] cmb@php.net
> So, I stand by my request :)

Apparently, nobody else does.  So if you're still interested in
this, please forward the request to the internals mailing list.
For the time being, I'm suspending this ticket.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC