Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110668 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 77041 invoked from network); 18 Jun 2020 18:18:57 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 18 Jun 2020 18:18:57 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4576218056B for ; Thu, 18 Jun 2020 10:04:55 -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-lj1-f171.google.com (mail-lj1-f171.google.com [209.85.208.171]) (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 10:04:54 -0700 (PDT) Received: by mail-lj1-f171.google.com with SMTP id y11so8141828ljm.9 for ; Thu, 18 Jun 2020 10:04:54 -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=bfHJ+XwL515iGJx12GUA4TN86q2QHvtywjNfaSJ4sc0=; b=SWmQYEZ/uOe1CpGay40ey9AoiDffSetD/I16QfGXmHPVgr0rumkBhgla7oBX1RJOou 3BG2GISDdON1J92/Hr9AaTDdE0xTl/MyXKU9g4jrHBLqo0zKuq6KJOI0sRsSReVPjwJR igY86gQdxheKFybTdiFftNijYD3HdPj6FJkk5nyBzG9UE3EMmuqPUVnhKp2JDP6atlcl gqmC1+pY36CeNVaSnl4ehitGmD2B4u6jvDE/X09X3CEwr5Sq/71YkHRoeBkdK7z80eVr OX+qnp9mjfZIfImF2gf+j+mfzaPHfr/5n8waifEINfduvnjlKbc/m1vN2zMfZ5Q/n1zh Nr+Q== 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=bfHJ+XwL515iGJx12GUA4TN86q2QHvtywjNfaSJ4sc0=; b=oNS0M0glYRC+ncfybwT9f0oX38GulpAEjvHosznncS62csYsvNXD9bSO7Om63QzD70 k6KmdcyKv4E4mZ5kgzGlEx4DIer50Ctx5oLE9bDIcFWjt8HP6Yz7L8nU3ExompeJQJiK Fi4oRvWbRr8u8P330jtjLEGijd3ogGsE9S9bzUCKfydcWsAHaRVQKJEhyg12ZWd74pmd BQ4UHThWCCF4QDehqVu3W9vOw5zc8WaMZwrqGLPOE9j/DfVDUT34ZSPsX4wR7LBlsvK1 aDPdG4QtMqhcb5TUUN4LRHn1yBCvvCKv0C1chKdJ2r42+xyMjjaZNTAHHRcPAkk70kNU hu0g== X-Gm-Message-State: AOAM531OkppdGARTkCGb0cNM5+pi5nxHZHh5iwcptbdRW1cs1yndfI7F 25NC4le5PdreNbz9mWZxUk5FQ+HN4o58P+nmknM= X-Google-Smtp-Source: ABdhPJx+9vwdL+RylBkmQIW68Pv3Hw5xaLvbrXRwWUryPBml+ItjgjSA44JWUyNmUTBj7p8Es+N76gRJYVcBRF+ceV8= X-Received: by 2002:a2e:98cb:: with SMTP id s11mr2724763ljj.402.1592499888053; Thu, 18 Jun 2020 10:04:48 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 18 Jun 2020 20:04:26 +0300 Message-ID: To: Bob Weinand Cc: PHP Internals List Content-Type: multipart/alternative; boundary="00000000000021e26905a85ec6e5" Subject: Re: [PHP-DEV] [RFC] [DISCUSSION] Make constructors and destructors return void From: benas.molis.iml@gmail.com (Benas IML) --00000000000021e26905a85ec6e5 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hey Bob, Your examples (regarding constructors/destructors) do make sense since you are using these methods for what they should be used: initialization and freeing resources. But, this is off-topic to the RFC. This RFC only proposes to enforce no-return rule on constructors/destructors. And based on my previous reply, top 2,000 Composer packages that have a BC break don't actually return anything (early returns and returns that don't change code behavior don't count). Your examples regarding `__toString`, `__invoke` and `__serialize` are also not relevant to this RFC. Although, I do understand that you were responding to email and I'm thankful for that :) Best regards, Benas On Thu, Jun 18, 2020, 7:43 PM Bob Weinand wrote: > Hey, > > Am 18.06.2020 um 17:18 schrieb Benas IML : > > 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 > > > __construct() is invoked directly on parent calls, sometimes to > reinitialize an object or > after ReflectionClass::newInstanceWithoutConstructor. > > I invoke __destruct() directly when needing an early freeing of existing > resources. > > "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 ar= e > a couple of examples that show that no magic method is ever supposed to b= e > called directly: > ```php > // __toString > (string) $object; > > > I like using ->__toString() in favor of (string) casts when the variable > is guaranteed to be an object to highlight that and avoid magic-ness. > > // __invoke > $object(); > > > Same here, unless the object is a closure. > > // __serialize > serialize($object); > ``` > > > Can't argue much about that one, I never use serialize(). > > 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`). > > > Yeah, __clone() is odd. No idea why. > > 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. > > > It also is generally a bad idea to have side effects in constructors, but > _sometimes_ it is justified. Only because something mostly is a bad idea, > it is not always. > Also note that other languages completely forbid manual ctor calls. But > PHP doesn't (and for good reason, like after > using ReflectionClass::newInstanceWithoutConstructor). > > Bob > > 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 >> on the >> > feedback I have received, this PR implements full validation and >> implicitly >> > enforces `void` rules on constructors/destructors while also allowing = to >> > declare an **optional** explicit `void` return type. Note, that there >> is 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 engi= ne >> 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 mostl= y >> 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 > > > --00000000000021e26905a85ec6e5--