Re: constructors are callable as regular functions.
| From: | troels knak-nielsen | Date: | Thu, 02 Jul 2009 14:55:48 +0000 |
| Subject: | Re: constructors are callable as regular functions. | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-44660@lists.php.net to get a copy of this message | ||
On Thu, Jul 2, 2009 at 4:47 PM, Edward Z. Yang<ezyang@mit.edu> wrote:
> Excerpts from troels knak-nielsen's message of Thu Jul 02 10:14:18 -0400 2009:
>> I would have expected the second call to __construct() to yield an error..
>
> Why should it? Especially since this is idiomatic code:
>
> class A {
> public function __construct($a) {
> $this->a = $a;
> }
> }
>
> class B extends A {
> public function __construct($a, $b) {
> $this->b = $b;
> parent::__construct($a);
> }
> }
In that example, the object instance is not initialised when
parent::__construct() is called.
> __construct doesn't do anything like allocate memory. It just happens
> to get called when we do "new B(1, 2)"
I understand that. It's not a technical issue - It's more a matter of
language semantics. Constructors are used for initializing state on an
object. Basically, this behaviour makes it impossible to implement
immutable objects in php. It's not a huge deal - I don't remember ever
seen __construct() called directly.
--
troels