Re: Null-safe property access in interpolated strings

From: Date: Mon, 10 Aug 2020 19:17:14 +0000
Subject: Re: Null-safe property access in interpolated strings
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-111446@lists.php.net to get a copy of this message
> > On Aug 10, 2020 at 11:02 AM, <Ilija Tovilo (mailto:tovilo.ilija@gmail.com)> wrote: > > > > Hi Mike > $card_html = <<<HTML <div id="card-{$card->id}" > class="card {$card?->css}"> {$card->content} </div> HTML; Two things: 1. > We're solely talking about string interpolation without braces {}. You're using braces in > your example and this does indeed work right now. > > > > > > > Thank you for clarifying. That was not obvious to me. If it was explicitly mentioned in the thread and I missed it, I apologize. > > > 2. The semantics of ?-> are different than you're depicting them to be in this > example. ?-> will only short-circuit if $card is null, not when the property "css" is > not defined. Thus, if $card was null your example would've already failed at $card->id. > > > > > > > Good point. I was remembering a frequent problem but did not convey the use-case correctly so please let me update it: > > class="card {$card->attributes?->css}" > > > > > The only real information I was trying to convey was that when generating HTML there is > a frequent need to output a value if one exists but output nothing otherwise. So hopefully this time > I got the usage correct for this new feature. -Mike P.S. I would have tested it before sending, but I do not have a local PHP install that I can test this syntax with yet. > > > > > > > > > > > > > > > > > > > > > > > > > > >

« previous php.internals (#111446) next »