Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97729 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 19456 invoked from network); 12 Jan 2017 19:25:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Jan 2017 19:25:25 -0000 Authentication-Results: pb1.pair.com smtp.mail=php@fleshgrinder.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=php@fleshgrinder.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain fleshgrinder.com from 77.244.243.89 cause and error) X-PHP-List-Original-Sender: php@fleshgrinder.com X-Host-Fingerprint: 77.244.243.89 mx108.easyname.com Received: from [77.244.243.89] ([77.244.243.89:36229] helo=mx108.easyname.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 3E/78-50165-328D7785 for ; Thu, 12 Jan 2017 14:25:24 -0500 Received: from cable-81-173-135-7.netcologne.de ([81.173.135.7] helo=[192.168.178.20]) by mx.easyname.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1cRkzw-0004Px-M5 for internals@lists.php.net; Thu, 12 Jan 2017 19:25:20 +0000 Reply-To: internals@lists.php.net References: <0DE25BF8-D349-48EF-A83B-8837DD4AD1E0@gmail.com> <5efcc230-c67b-4439-cc82-b31eacbf36db@php.net> To: internals@lists.php.net Message-ID: <07fe33aa-cfd5-162a-ff88-f2b48c9efb3b@fleshgrinder.com> Date: Thu, 12 Jan 2017 20:25:18 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.6.0 MIME-Version: 1.0 In-Reply-To: <5efcc230-c67b-4439-cc82-b31eacbf36db@php.net> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-DNSBL-PBLSPAMHAUS: YES Subject: Re: [PHP-DEV] Explicit constructor call and polymorphic dispatch From: php@fleshgrinder.com (Fleshgrinder) On 1/12/2017 8:19 AM, Sebastian Bergmann wrote: > Am 12.01.2017 um 08:11 schrieb Tim Bezhashvyly: >> Disallow explicit call of __construct method > > I am baffled that this (still) works: > > $ cat /tmp/t.php > class C > { > public function __construct() > { > print __METHOD__ . PHP_EOL; > } > } > > $o = new C; > $o->__construct(); > > > $ php /tmp/t.php > C::__construct > C::__construct > > > $ php --version > PHP 7.1.0 (cli) (built: Dec 1 2016 07:39:00) ( NTS ) > Copyright (c) 1997-2016 The PHP Group > Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies > > I am in favor of an RFC to disallow explicit invocation of interceptor > methods such as __construct(). > > Can we do this in PHP 7.2 or does this have to wait for PHP 8? > https://github.com/php/php-langspec/blob/master/spec/14-classes.md#constructors > Constructors are called by object-creation-expression and from within > other (derived class) constructors. This suggests that creation is possible via the `new` keyword only and not via explicit calls to `__construct`. Hence, it could be categorized as unspecified and dropped in 7.2. However, there are other oddities that probably should be addressed too in the context of constructors which are actually defined and thus can only be removed in PHP 8. > A constructor can return a result, by value or byRef. > A constructor should not call its base-class constructor more than > once. ``` __construct(); ``` ``` $ php test.php A B A A B A PHP ``` Note that both PHP and HHVM implement this _correctly_: https://3v4l.org/DFGCa -- Richard "Fleshgrinder" Fussenegger