php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31669 no __construct() method in parent causes error...
Submitted: 2005-01-23 16:06 UTC Modified: 2005-01-24 01:58 UTC
From: jason at amp-design dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.* OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jason at amp-design dot net
New email:
PHP Version: OS:

 

 [2005-01-23 16:06 UTC] jason at amp-design dot net
Description:
------------
If you extend from a base class with no explict constructor defined, and the derived class has a call to parent::__construct(), an error is raised.

However it is perfectly legit to call new Dummy(), despite the fact Dummy has no explict constructor. Surely, if no constructor is defined in a base class, it should provide a default null/empty constructor to prevent errors. Without this, programmers have to provide a empty __construct method, which seems be silly as it serves no purpose other than to act as 'scaffolding code'.

I have checked in the PHP Manual under the OO bit for PHP4 and PHP5 to make sure this is not the intended behaviour for PHP. If this is not a bug and you have to provide dummy constructors, this should be made clear in the documentation.

If needed I can test this at home under Windows as well using a CVS snapshot if I get time. I'm sure it will produce the same result as it appears to be a language level issue.

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

class Dummy {
	// uncomment here to make me work :-)
	// function __construct() {}
}

class BillGates extends Dummy {
	function __construct() {
		parent::__construct();
	}
}

$new_idiot_to_dominate_world = new BillGates();

?>

Expected result:
----------------
Nothing should be output. $new_idiot_to_dominate_world should have an instance of BillGates.

Actual result:
--------------
Fatal error: Can not call constructor in BillGatesIsAScumBag.php on line 10

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-01-23 20:32 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

It is 'a language level issue' by design.
 [2005-01-24 01:58 UTC] jason at amp-design dot net
Well despite the fact that is so happens to be something that is supposed to happen "by design", it would be good if this could be reflected in the documentation if it is not seen as a bug (which I was expecting somewhat). The main reason I found this worthy of a bug report is the fact that this behaviour is different from that of C++. Something like 

class A {
};

class B : A {
public:
	B(void) {
		A::A();
	}
};

int main(int argc, char **args)
{
	B b = *new B();
	return 0;
}

Will compile fine, and not have any issues during run time. I personally can't see how it can be such as issue for PHP to follow C++ and remain consistent.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Sep 02 09:00:02 2025 UTC