Re: constructors are callable as regular functions.
| From: | Greg Beaver | Date: | Thu, 02 Jul 2009 15:05:28 +0000 |
| Subject: | Re: constructors are callable as regular functions. | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-44662@lists.php.net to get a copy of this message | ||
troels knak-nielsen wrote:
> 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
Hi,
__construct can be private or protected, it just requires implementing a
factory() or singleton() method to retrieve the immutable object.
Greg