Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110663 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 58781 invoked from network); 18 Jun 2020 16:32:29 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 18 Jun 2020 16:32:29 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 828201804A8 for ; Thu, 18 Jun 2020 08:18:25 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 18 Jun 2020 08:18:25 -0700 (PDT) Received: by mail-lf1-f50.google.com with SMTP id t74so3711900lff.2 for ; Thu, 18 Jun 2020 08:18:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Gpma2lvNxAllvZqU5rvIwLWH1LU9ttIQZOs2ckiakzE=; b=H8La0EAmGhm+twb6nIfXUyh4C8leyd0naZY4GHProzPoZs6707DgzXnPfBqUwNVazL vyehYIKTyLrXtAaU3Qcf522rpuKd0VseG1mngaNhH3SJw+bGL2kT3aT5Pf7saws1aZB2 i984H5fMxfi9YtAZNH4x162lDfGteqCbnmGLCLZk3CnAk/elG0jAIoaYEJsCjprifoBw Xums7bLK+B8xQ9LlslFKCq+tK4NVLDlc0xF8jhRsTIXdsLwo3JXMigBNFCtLbRD1rPz5 6wAvJ7UtTsG9PK4aiuyPhJvECmitoVVTja9mq1WV/9eX5cesAu+9TbjPBoNJ6iwDooJ9 UWAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Gpma2lvNxAllvZqU5rvIwLWH1LU9ttIQZOs2ckiakzE=; b=ul7dpnjz8Ubh6Xxfcuu/6f2cEnDfY/RPrxU4tDuD47fQ+UhLfovTmWf91BwVqLohqQ sUp++Faui1EFVaDLgy8u4/k5Zx6AGRindzpPcqPHDl1D0zUU5fLcZ0AlE5aRu7GWn6Lt k3N06AxrxYiRVa1JFxLbhoOeWpW63y8n0vHQ/A/TBhZgk7mbPSxRSUgpVle7pzjL/5C+ Z4bjymkH0aTTr0pi6gXlRSb0dp23gCXDKb6VppvszTWAxtF9AkoNzrUtk2P0JBkY7ia6 /mdNBLeMxEM4fQ5/z+a/V1WLjwGERFozGQhUMr2tIcaAshxRLwK6bj91FbLPmNiZdGg4 5pxg== X-Gm-Message-State: AOAM531BICOGo3z51aLNru1HgyGttGEy9hlRqpoyX0YQxc945fFOGW/+ 0OICE3JX83FYvesDc6VrCzeWHeb3ferh3WYbKsM= X-Google-Smtp-Source: ABdhPJwqwS7hgnFxpdKsd4LWbPg0YBUF75vfdQaJtYoZyX8SsmhK6coEi81JrdpBxe7xMLzgHmouIrWyc6V4tglOrR0= X-Received: by 2002:a19:3f01:: with SMTP id m1mr2627021lfa.130.1592493503641; Thu, 18 Jun 2020 08:18:23 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 18 Jun 2020 18:18:10 +0300 Message-ID: To: Bob Weinand Cc: PHP Internals List Content-Type: multipart/alternative; boundary="000000000000977b8705a85d490b" Subject: Re: [PHP-DEV] [RFC] [DISCUSSION] Make constructors and destructors return void From: benas.molis.iml@gmail.com (Benas IML) --000000000000977b8705a85d490b Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hey Bob, Magic methods are **never** supposed to be called directly (even more if that method is a constructor or a destructor). If that's not the case, it's just plain bad code. But by enforcing these rules, we make sure that less of that (bad code) is written and as a result, we make PHP code less bug-prone and easier to debug. That's also most likely the reason why "ensure magic methods' signature" RFC opted in to validate `__clone` method's signature and ensure that it has `void` return type. Just for the sake of making sure that you understand what I mean, here are a couple of examples that show that no magic method is ever supposed to be called directly: ```php // __toString (string) $object; // __invoke $object(); // __serialize serialize($object); ``` Moreover, by validating constructors/destructors and allowing an explicit `void` return type declaration, we are becoming much more consistent (something that PHP is striving for) with other magic methods (e. g. `__clone`). Also, saying that "sometimes you have valid information to pass from the parent class" is quite an overstatement. After analyzing most of the 95 Composer packages that had a potential BC break, I found out that either they wanted to return early (that is still possible to do using `return;`) or they added a `return something;` for no reason. Thus, no libraries actually returned something useful and valid from a constructor (as they shouldn't). Last but certainly not least, constructors have one and only one responsibility - to initialize an object. Whether you read Wikipedia's or PHP manual's definition, a constructor does just that. It initializes. So, the PHP manual is perfectly correct and documents the correct return type that a constructor should have. Best regards, Benas On Thu, Jun 18, 2020, 4:06 PM Bob Weinand wrote: > > Am 17.06.2020 um 01:10 schrieb Benas IML : > > > > Hey internals, > > > > This is a completely refined, follow-up RFC to my original RFC. Based o= n > the > > feedback I have received, this PR implements full validation and > implicitly > > enforces `void` rules on constructors/destructors while also allowing t= o > > declare an **optional** explicit `void` return type. Note, that there i= s > a > > small but justifiable BC break (as stated by the RFC). > > > > RFC: https://wiki.php.net/rfc/make_ctor_ret_void > > > > Best regards, > > Benas Seliuginas > > Hey Benas, > > I do not see any particular benefit from that RFC. > > Regarding what the manual states - the manual is wrong there and thus > should be fixed in the manual. This is not an argument for changing engin= e > behaviour. > > Sometimes a constructor (esp. of a parent class) or destructor may be > called manually. Sometimes you have valid information to pass from the > parent class. > With your RFC an arbitrary restriction is introduced necessitating an > extra method instead. > > In general that RFC feels like "uh, __construct and __destruct are mostly > void, so let's enforce it =E2=80=A6 because we can"? > > On these grounds and it being an additional (albeit mostly small) > unnecessary BC break, I'm not in favor of that RFC. > > Bob --000000000000977b8705a85d490b--