Re: instance version of match ?

From: Date: Tue, 29 Mar 2022 10:59:01 +0000
Subject: Re: instance version of match ?
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-117442@lists.php.net to get a copy of this message
On Tue, Mar 29, 2022 at 3:47 AM Bruce Weirdan <weirdan@gmail.com> wrote: > > On Mon, Mar 28, 2022 at 7:56 PM Karoly Negyesi <karoly@negyesi.net> wrote: > > match ($object) { > > Someinterface => 'foo', > > AnotherInterface => 'bar', > > } > > > > this can not clash with any existing code as using identifiers like this > > are a syntax error currently. > > That's valid code actually, see https://3v4l.org/BEcE4 > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > It can be modified to produce ambiguous code however: const SomeInterface = 1; const AnotherInterface = 2; interface SomeInterface {} interface OtherInterface {} $object = new class implements SomeInterface {}; var_dump( match (true) { $object instanceof SomeInterface => 'foo', $object instanceof AnotherInterface => 'bar', } ); We can see that SomeInterface will resolve the interface and not the constant. I think what they are proposing is that when the match is an object, and the branches are class/interface/etc names, it should just do an instanceof operation instead of a value-equals operation.

« previous php.internals (#117442) next »