Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125971 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id DBEB81A00BD for ; Sat, 16 Nov 2024 09:24:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1731749209; bh=qk+AyxSpmkjPhTulKgJZ4NjBiyI2DOEol4t39yGqaXI=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From; b=Aa6QqjVdPDKkrk+nhDtQA8rcnN4bWTyj3tWAOxvgKkGusInrGmb21L8ElUEOA3P+v dMLKqxASAOCRaSfOTKge10rcJcN4V87R+UKDeiWuyJCFsdG6WMQZLlkng8qiFNVbtk s2ql9YHJwtnYtIpfnRfnSHsbXkxqWPhGCUNdhF30R7SDjT/ikZJde7094IC2OSyEqm 4uc+9Ml3qFSbdt3OxFcPax4vZv6IJiFHBiiaePm5fbykhsKNUzhUEBZfpgWRTmkRxl OQHE+0In2wDc9LPgMMBitLHZ43wcXVrlBzBOSFTgVwuJlLegc574j+X5+rMOA8bl/z 14a/7EnU0UfvQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E087E18005B for ; Sat, 16 Nov 2024 09:26:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-a5-smtp.messagingengine.com (fhigh-a5-smtp.messagingengine.com [103.168.172.156]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 16 Nov 2024 09:26:48 +0000 (UTC) Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfhigh.phl.internal (Postfix) with ESMTP id CAAC8114016A; Sat, 16 Nov 2024 04:24:09 -0500 (EST) Received: from phl-imap-09 ([10.202.2.99]) by phl-compute-01.internal (MEProxy); Sat, 16 Nov 2024 04:24:09 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:cc:content-type:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm1; t=1731749049; x= 1731835449; bh=Nxh+v1Qxv9rARTRUHiNka/hJVD7KNCpjyoN9aynh2Uc=; b=i 2AkJfEjjNVRhFlX/nmwXf8P67G7z2sqAcNc4883qXj12utXX2OfVnfWByTDvprSn KUOwVtg4G1ScdVZp8w8Fk6k8zjBq7Mjf+jmxeFLufrt16tu7YprJbk8syx0KIIPu RA501m856B8VoaZRI9Z2pVo6C7LWdYyFR+YykPuiwvkUXrcdZiie+bqe4yZN035A AlyeMUfYIwuw40PCqSU08++dTcjeD6dPLNWsq2QJcVgjdxroKLRbmDHEQMlWyIgc r7J82xH00BIUWvfnlAV0FHqO4J6GXjsgXFtxjq7TiGTktz+Nqd1QmJri2Gijj5D1 1IJ499IIilbLSthldZldQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t= 1731749049; x=1731835449; bh=Nxh+v1Qxv9rARTRUHiNka/hJVD7KNCpjyoN 9aynh2Uc=; b=B0Ysto3MGQVupdKj4j3/f5l4NWX3zGKRO6bUhX+x31VN1BYq2zi exlXNuGQFPGAUuoGYbtfwym/7MTsgLE1eX1lAZG7ukkk4w99mONmJ8uRUQDwBxtb F6TOezozbrgQhSC7yobNFxg8py7U3P0RHpmeXH5B2oakiJteCaVxnflAo/0zHwqR FEtfltRMqE5AN8Ah3J02IVUsJfdvNIgK4VGg5QMbOkRLRdS90qTJRU9j/OgEsEqY zbaTYZ97eWFKtY19/MjcLZX4sUojZEOt9cgrYnsItRKelRFKtZnzOQWuEd/8n95R mp4NT7l8ZiDvotorAXqqKt5vJTOJc+p1elA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrvdeigddtudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnth hsucdlqddutddtmdenogfuuhhsphgvtghtffhomhgrihhnucdlgeelmdenucfjughrpefo ggffhffvvefkjghfufgtsegrtderreertdejnecuhfhrohhmpedftfhosgcunfgrnhguvg hrshdfuceorhhosgessghothhtlhgvugdrtghouggvsheqnecuggftrfgrthhtvghrnhep hedtvdekvdehffeuheehgfffheejhfekudehvdegleeliedvfeeuleektdegjeegnecuff homhgrihhnpehsthgrtghkohhvvghrfhhlohifrdgtohhmpdefvheglhdrohhrghenucev lhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehrohgssegsoh htthhlvggurdgtohguvghspdhnsggprhgtphhtthhopedvpdhmohguvgepshhmthhpohhu thdprhgtphhtthhopeguvghlvghughihnhesghhmrghilhdrtghomhdprhgtphhtthhope hinhhtvghrnhgrlhhssehlihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 80AB6780068; Sat, 16 Nov 2024 04:24:09 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Sat, 16 Nov 2024 10:23:49 +0100 To: =?UTF-8?Q?Marco_Aur=C3=A9lio_Deleu?= Cc: internals@lists.php.net Message-ID: <98d66def-55cd-43f3-9d01-e2c58f5c0869@app.fastmail.com> In-Reply-To: References: Subject: Re: [PHP-DEV] Optional interfaces Content-Type: multipart/alternative; boundary=ea888a70899b4e9e8da59c2b7a61bb98 From: rob@bottled.codes ("Rob Landers") --ea888a70899b4e9e8da59c2b7a61bb98 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Sat, Nov 16, 2024, at 01:27, Marco Aur=C3=A9lio Deleu wrote: >=20 >> On 15 Nov 2024, at 20:42, Rob Landers wrote: >> =EF=BB=BF >> On Sat, Nov 16, 2024, at 00:34, Juris Evertovskis wrote: >>> Hey all, >>> =20 >>> If you try to implement an interface that does not exist, you get th= e =E2=80=98Interface =E2=80=9C%s=E2=80=9D not found=E2=80=99 error. Usua= lly that=E2=80=99s useful as it points to a code error. >>> =20 >>> However, sometimes we want our code to be compatible with an optiona= l library. For example, my Expression class might be compatible with som= e ORM, but it would also usable without it or with another ORM. This can= already be accomplished, but the solutions seems very workaroundish: ht= tps://stackoverflow.com/questions/57996429/implements-interface-only-if-= exists >>> =20 >>> Maybe the PHP itself should allow marking some interfaces as optiona= l =E2=80=94 such that they are discarded from the list if the interface = class does not exist? Here=E2=80=99s one way the syntax could look like: >>> =20 >>> class Expression implements MyInterface, ?OptionalInterface {} >>> =20 >>> interface EventDispatcherInterface extends ?PsrEventDispatcherIn= terface {} >>> =20 >>> BR, >>> Juris >>=20 >> Hey Juris, >>=20 >> Is there some reason you cannot do this: >>=20 >> https://3v4l.org/hF1vr >>=20 >> =E2=80=94 Rob >=20 > That seems to match what he shared on SO link? Thanks Marco, I don't visit stackoverflow because I loath "features locked behind inte= rnet points". Thanks for letting me know what is behind the link! >=20 > I think I opened this email with a strong negative feeling towards wha= t would be optional interface, but to be honest a syntax sugar where imp= lements ?MyInterface becomes something pretty close to what Rob shared d= oesn=E2=80=99t sound bad, it=E2=80=99s just convenience. Unfortunately P= HP is not keen on being open to those. >=20 > Overall, I dig it. I maintain a PHP SDK that needs to be compatible with Symfony and Larave= l and any other framework. The shenanigans are all bound in one file tha= t I very rarely touch. So, I do kinda like the idea of having optional i= nterfaces, but I feel like it is rather niche. As an application develop= er, I can't think of one time I've ever needed this. However, the single biggest advantage comes from IDEs that can somehow s= how that there is an optional interface on a type. I think that would be= the most useful bit to come from this, honestly. It also prevents IDEs = or static analysis from giving a warning that there are multiple definit= ions of an interface if you have the appropriate libraries installed. It's also rather simple to implement, so I think it is worth pursuing, I= MHO. =E2=80=94 Rob --ea888a70899b4e9e8da59c2b7a61bb98 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
On Sat, Nov 16,= 2024, at 01:27, Marco Aur=C3=A9lio Deleu wrote:

On 15 Nov 2024, at 20:42, Rob Lander= s <rob@bottled.codes> wrote:
=EF=BB=BF
On Sat, N= ov 16, 2024, at 00:34, Juris Evertovskis wrote:

Hey all,

 

If you= try to implement an interface that does not exist, you get the =E2=80=98= Interface =E2=80=9C%s=E2=80=9D not found=E2=80=99 error. Usually that=E2= =80=99s useful as it points to a code error.

 

However, sometimes w= e want our code to be compatible with an optional library. For example, = my Expression class might be compatible with some ORM, but it would also= usable without it or with another ORM. This can already be accomplished= , but the solutions seems very workaroundish: https= ://stackoverflow.com/questions/57996429/implements-interface-only-if-exi= sts

 

Maybe the PHP itself should allow marking some interfaces= as optional =E2=80=94 such that they are discarded from the list if the= interface class does not exist? Here=E2=80=99s one way the syntax could= look like:

 

    class Expression implements MyInter= face, ?OptionalInterface {}

 

    interface EventDis= patcherInterface extends ?PsrEventDispatcherInterface {}

 

BR,
<= /p>

Juris

Hey Juris,

Is there some reason= you cannot do this:


=E2=80=94 Rob

That seems to match what he shared on SO link?

Thanks Marco,

I don't visit stackoverflow because I loath "features locked behind int= ernet points". Thanks for letting me know what is behind the link!

=

I think I opened this email with a strong negative f= eeling towards what would be optional interface, but to be honest a synt= ax sugar where implements ?MyInterface becomes something pretty close to= what Rob shared doesn=E2=80=99t sound bad, it=E2=80=99s just convenienc= e. Unfortunately PHP is not keen on being open to those.
<= br>
Overall, I dig it.

I maintain a PHP SDK that needs to be compatible with Symfony and Lar= avel and any other framework. The shenanigans are all bound in one file = that I very rarely touch. So, I do kinda like the idea of having optiona= l interfaces, but I feel like it is rather niche. As an application deve= loper, I can't think of one time I've ever needed this.
However, the single biggest advantage comes from IDEs that = can somehow show that there is an optional interface on a type. I think = that would be the most useful bit to come from this, honestly. It also p= revents IDEs or static analysis from giving a warning that there are mul= tiple definitions of an interface if you have the appropriate libraries = installed.

It's also rather simple to imple= ment, so I think it is worth pursuing, IMHO.

=E2=80=94 Rob
--ea888a70899b4e9e8da59c2b7a61bb98--