Req #64454 [Com]: It is possible to directly call the magic method "__construct".

From: Date: Fri, 22 Mar 2013 07:03:54 +0000
Subject: Req #64454 [Com]: It is possible to directly call the magic method "__construct".
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-177990@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=64454&edit=1

 ID:                 64454
 Comment by:         nikic@php.net
 Reported by:        wolters dot fl at gmail dot com
 Summary:            It is possible to directly call the magic method
                     "__construct".
 Status:             Wont fix
 Type:               Feature/Change Request
 Package:            Class/Object related
 Operating System:   Microsoft Windows Version 6.2.9
 PHP Version:        5.4.13
 Block user comment: N
 Private report:     N

 New Comment:

Additionally to what laruence said, one should also point out that __construct is quite commonly
called directly in the form of parent::__construct().


Previous Comments:
------------------------------------------------------------------------
[2013-03-22 04:18:32] laruence@php.net

PHP's __construct, actually, is not creating object, it decorate the object after 
it was created.

so,  even you directly call to $obj->__construct, then no new instance was create.

so a workaround is you can exam some property you set to see whether that is a 
double constructing or not.


thanks

------------------------------------------------------------------------
[2013-03-19 12:45:54] wolters dot fl at gmail dot com

Description:
------------
It is possible to directly call the magic method "__construct". This should NOT be
possible, since one cannot create immutable objects with the current behaviour.

In addition, a direct call to the magic method "__clone" is not possible and raises an
"\E_FATAL". Both magic methods deal with object creation, so the behaviour is inconsistent
between them.

I am using "php-5.4.13-nts-Win32-VC9-x86" on Microsoft Windows Version 6.2.9 x64(Windows 8
64 bit).

Test script:
---------------
https://gist.github.com/FlorianWolters/5195734

Expected result:
----------------
The method call:

$example->__construct();

should raise an "\E_FATAL" with the following message:

"Cannot call __construct() method on objects - use 'new <Class>' instead in
[...] on line 21"



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=64454&edit=1


Thread (5 messages)

« previous php.bugs (#177990) next »