Req #55864 [Opn]: Explicit call to default constructor
Edit report at https://bugs.php.net/bug.php?id=55864&edit=1
ID: 55864
Updated by: laruence@php.net
Reported by: dagguh at gmail dot com
Summary: Explicit call to default constructor
Status: Open
Type: Feature/Change Request
Package: Class/Object related
Operating System: Ubuntu 10.04.3 LTS
PHP Version: 5.3.5
Block user comment: N
Private report: N
New Comment:
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 :)
Previous Comments:
------------------------------------------------------------------------
[2011-10-07 09:55:01] 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 09:41:18] 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:25:40] dagguh at gmail dot com
Fixed version and OS
------------------------------------------------------------------------
[2011-10-07 09:24:50] dagguh at gmail dot com
PS. Actually my PHP version is 5.3.5
------------------------------------------------------------------------
[2011-10-07 09:20:34] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55864&edit=1
Thread (8 messages)