# Null-safe property access in interpolated strings

- Thread: https://externals.io/message/111383
- Original archive: https://news-web.php.net/php.internals/111383
- Started by Sara Golemon on 2020-08-08T22:17:35+00:00
- Messages: 10
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #111383 — Sara Golemon — 2020-08-08T22:17:35+00:00

URL: https://externals.io/message/111383#111383

Do we expect this to work?

$foo = new stdClass;
$foo->bar = "Hello";
echo "$foo?->bar world\n";

Because at the moment it doesn't: https://3v4l.org/nLv3l

-Sara

---

## #111386 — Marco Pivetta — 2020-08-09T03:47:48+00:00

URL: https://externals.io/message/111383#111386 · In reply to: #111383

> Do we expect this to work?
>
> $foo = new stdClass;
> $foo->bar = "Hello";
> echo "$foo?->bar world\n";
>
> Because at the moment it doesn't: https://3v4l.org/nLv3l
>
> -Sara
>

Ooof, people still interpolate strings that way?

Good riddance if it doesn't work: this code is questionable (and not just
because of the question mark in it) 👍

---

## #111389 — Yousuf Tafhim — 2020-08-09T11:49:22+00:00

URL: https://externals.io/message/111383#111389 · In reply to: #111386

What are the good uses cases for this to work?



> 
>
> > […]
>
> Ooof, people still interpolate strings that way?
>
> Good riddance if it doesn't work: this code is questionable (and not just
> because of the question mark in it) 👍

---

## #111398 — Ilija Tovilo — 2020-08-09T18:32:50+00:00

URL: https://externals.io/message/111383#111398 · In reply to: #111383

Hi internals

> Do we expect this to work?
>
> $foo = new stdClass;
> $foo->bar = "Hello";
> echo "$foo?->bar world\n";
>
> Because at the moment it doesn't: https://3v4l.org/nLv3l

The patch can be found here:
https://github.com/php/php-src/pull/5966

It adds ~5 lines to the lexer. As mentioned, there is a BC break
although it is probably very small (I could find no code that breaks
on grep.app). Because this BC break was never mentioned in the RFC I'm
leaning towards not merging this patch but I am happy either way.

Let's hear from a few more people what they think until we decide how
to move forward.

Ilija

---

## #111399 — Jordi Boggiano — 2020-08-09T18:34:24+00:00

URL: https://externals.io/message/111383#111399 · In reply to: #111383

> Do we expect this to work?
>
> $foo = new stdClass;
> $foo->bar = "Hello";
> echo "$foo?->bar world\n";
>
> Because at the moment it doesn't: https://3v4l.org/nLv3l
>
> -Sara

Can't say I'm big on interpolation but I'd definitely expect this to 
work because why not?

I think if it can be reasonably fixed it probably would make sense for 
consistency and WTF-avoidance if anything.

Best,
Jordi

-- 
Jordi Boggiano
@seldaek - https://seld.be

---

## #111401 — Deleu — 2020-08-09T19:00:14+00:00

URL: https://externals.io/message/111383#111401 · In reply to: #111399

I like and make use of interpolation, but I can't think of a use case for
this. Is there any valid use case that would benefit from this fix
regardless of personal preference? In other words, where would one use
string interpolation with an empty string being a valid case?



> 
> > […]
>
> Can't say I'm big on interpolation but I'd definitely expect this to
> work because why not?
>
> I think if it can be reasonably fixed it probably would make sense for
> consistency and WTF-avoidance if anything.
>
> Best,
> Jordi
>
> --
> Jordi Boggiano
> @seldaek - https://seld.be
>
> --
> 
> To unsubscribe, visit: https://www.php.net/unsub.php

---

## #111420 — Nikita Popov — 2020-08-10T10:33:22+00:00

URL: https://externals.io/message/111383#111420 · In reply to: #111399

> 
> > […]
>
> Can't say I'm big on interpolation but I'd definitely expect this to
> work because why not?
>
> I think if it can be reasonably fixed it probably would make sense for
> consistency and WTF-avoidance if anything.
>
> Best,
> Jordi
>

Agree. I don't think the question of whether it is useful should come into
this, it's a matter of language consistency. There could be some leeway
here if we say that we have plans to deprecate the "$x->y" syntax in the
future anyway and don't want to extend it anymore -- but I don't believe we
have such plans at the present time.

Nikita

---

## #111421 — Philip Hofstetter — 2020-08-10T11:02:16+00:00

URL: https://externals.io/message/111383#111421 · In reply to: #111399

Hi,



> Can't say I'm big on interpolation but I'd definitely expect this to
> work because why not?

A reason why not is because it will break backwards compatibility with
existing (though admittedly unlikely) code which also can't be fixed
by easy search and replace:

    $foo = "gnegg";
    echo "$foo?->bar()"

would be fine in PHP < 8 and would blow up with `Uncaught Error: Call
to a member function bar() on string` with this change.

I'm not saying this is a problem because code like this is unlikely to
be written, but it *is* a BC break.

Philip

---

## #111441 — Ilija Tovilo — 2020-08-10T15:06:50+00:00

URL: https://externals.io/message/111383#111441 · In reply to: #111420

Hi Nikita

> > […]
>
> Agree. I don't think the question of whether it is useful should come into
> this, it's a matter of language consistency. There could be some leeway
> here if we say that we have plans to deprecate the "$x->y" syntax in the
> future anyway and don't want to extend it anymore -- but I don't believe we
> have such plans at the present time.

So for the sake of consistency let's merge this then. As mentioned,
the BC break should be very very small. A few people have mentioned
they didn't expect it to work but when asked again they didn't feel
strongly about it.

Unless there are objections I will merge this tomorrow. A review of
the PR would also be welcome.

Ilija

---

## #111447 — bjorn.x.larsson@telia.com — 2020-08-10T21:43:04+00:00

URL: https://externals.io/message/111383#111447 · In reply to: #111441

Hi Ilija,

Den 2020-08-10 kl. 17:06, skrev Ilija Tovilo:

> Hi Nikita
>
> > […]
> So for the sake of consistency let's merge this then. As mentioned,
> the BC break should be very very small. A few people have mentioned
> they didn't expect it to work but when asked again they didn't feel
> strongly about it.
>
> Unless there are objections I will merge this tomorrow. A review of
> the PR would also be welcome.
>
> Ilija

I second Nikitas opinion that consistency is important here. One shouldn't
need to spend time as an PHP end user wondering why we have a special
case that doesn't work like the rest!

Good luck with merging.

r//Björn L
