Re: instance version of match ?

From: Date: Tue, 29 Mar 2022 13:18:13 +0000
Subject: Re: instance version of match ?
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-117444@lists.php.net to get a copy of this message
On Tuesday, March 29th, 2022 at 12:55 AM, Karoly Negyesi <karoly@negyesi.net> wrote: > Hi, > > May I suggest adding a little "instanceof" magic to match? Like this: > > match ($object) { > > Someinterface => 'foo', > > AnotherInterface => 'bar', > > } > > this can not clash with any existing code as using identifiers like this > > are a syntax error currently. > > Currently you could do the same with > > match (true) { > > $object instanceof Someinterface::class => 'foo', > > $object instanceof AnotherInterface::class => 'bar', > > } > > Indeed, the former could be considered syntactic sugar for the latter. And > > I do not think there's any ambiguity here for the casual reader or is it > > just me who thinks no other sensible reading is possible? > > Thanks for your consideration. > > Karoly Negyesi Hi Karoly, As mentioned by Bruce, this syntax is already valid, therefor, it can't be used. what you are looking for is pattern-matching, see "match() enhancement" section in the RFC: https://wiki.php.net/rfc/pattern-matching Regards, Saif Eddin Gmati.

Attachment: [application/pgp-signature] OpenPGP digital signature signature.asc
« previous php.internals (#117444) next »