Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108273 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 57899 invoked from network); 27 Jan 2020 17:45:44 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 27 Jan 2020 17:45:44 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 81120180568 for ; Mon, 27 Jan 2020 07:55:56 -0800 (PST) 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.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS11403 66.111.4.0/24 X-Spam-Virus: No X-Envelope-From: Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 27 Jan 2020 07:55:56 -0800 (PST) Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 2F76322015 for ; Mon, 27 Jan 2020 10:55:56 -0500 (EST) Received: from imap26 ([10.202.2.76]) by compute7.internal (MEProxy); Mon, 27 Jan 2020 10:55:56 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=6VyQWa Q29Y5CpXed4SfwvDXOHwqBlRFZHMKvSc8o0hg=; b=vtoqNkGDN8iUhNSHcP1Oyd HW1Xu6xpZQSYwdD1GxILoZz57BZiBgJVuJF5nYw3wWxOYA0qaKLjS7Pei9z8wSiM kOK98tZ96B4Tc4+ELYxPL1nocL3LLEjkW1P5O6X9sHlcV7MrCmI/Yoactop6JEmz FgIM7S73xlb8fqRUyrpKx64ayu40IItzFwe/yZgaQqr+ira2Z0CnQkvdJXzgEvD+ ATIe8WKBrjUWMefQQU9P2LNl9ndyVWkAAGJVlEU3KO6TxXScuVEABuk9lJvzvMwp Bc2XMKcguBrjLyEja1qPjmTwEA2c2BdFLE1cH64PsFmS3Nu6zXuMtrA+1dJR0HEA == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrfedvgdektdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefofgggkfgjfhffhffvufgtsehttdertderredtnecuhfhrohhmpedfnfgrrhhr hicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh eqnecuffhomhgrihhnpehphhhprdhnvghtpdhgihhthhhusgdrtghomhenucevlhhushht vghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehlrghrrhihsehgrghrfh hivghlughtvggthhdrtghomh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id CE13514200A2; Mon, 27 Jan 2020 10:55:55 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.1.7-777-gdb93371-fmstable-20200123v1 Mime-Version: 1.0 Message-ID: In-Reply-To: References: Date: Mon, 27 Jan 2020 16:55:35 +0100 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] [RFC] Adding a "Stringable" interface to PHP 8 From: larry@garfieldtech.com ("Larry Garfield") On Mon, Jan 27, 2020, at 10:19 AM, Nikita Popov wrote: > On Wed, Jan 22, 2020 at 4:47 PM Nicolas Grekas > wrote: > > > Hello everyone, > > > > as announced last week, I'm officially opening a discussion for adding a > > "Stringable" interface to PHP 8. > > > > The RFC and its rationale are presented here as required: > > https://wiki.php.net/rfc/stringable > > > > The patch is found on GitHub, where some discussions happened already, you > > might be interested in having a look: > > https://github.com/php/php-src/pull/5083 > > > > TL;DR, I think we need an interface to allow passing objects with a > > __toString() method in a type-safe way, using the string|Stringable type. > > Here is the stub of the proposal: > > interface Stringable { public function __toString(): string; } > > > > I invite everyone to review the RFC before commenting here as I tried to > > sum up considerations that some already had before and that you might have > > too. > > > > Cheers, > > Nicolas > > > > I'm wondering whether it could make sense to automatically implement this > interface for all classes that define __toString(). Explicitly implementing > the interface is useful for the transition period (where old PHP versions > also need to be supported), but when considering only PHP 8 support, I > think it may make sense to implicitly implement the interface. If the > __toString() functionality also works fine without implementing the > interface, and there's probably only going to be relatively few consumers > of the Stringable type, it will likely be quite common that the interface > will not get implemented... > > Automatically implementing interfaces based on implemented methods could > also be a general language feature (see Go interfaces for example), but > likely not one we're interested in having in it's full generality. In this > particular case, it seems useful though. > > Regards, > Nikita I believe Anthony Ferrara proposed "Weak interfaces" once many years ago, which is essentially what you're describing. It didn't get much traction although I'm unclear why. (Possibly just less familiarity with Go at the time?) I agree not all interfaces should be implicit, but having implicitly-implementable interfaces does sound generally useful. That shouldn't block this RFC, but might this RFC be a model for how to implement such things more generically in the future? --Larry Garfield