Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:130928 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 lists.php.net (Postfix) with ESMTPS id 061411A00BC for ; Sun, 17 May 2026 18:42:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1779043366; bh=0wagC/vvdE2Orj+rN4Ld9P145HNTUZGjktNlqjFaw8s=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=hhdFB1IWuLiL6y/jZs3cQwUu7q2x2wbhxxtYUjIuPDT/ASbnLUNu+XLeYsBcFaEXW 3TlLpYZcdgLDPAGAXF15/cjigP6+IFxTNImaSRf05SxzhR8s8BrWk8L8+6ipTaRnbz nERDwY7jfiVZimJPKgNfrO8wZGuNTKsicEl5FoScG89ol6QY+SR+hGyOzeFGJI3yRz Hl88OyPRY+6K3widzF6TZ2/ODRzZE7mcgs6yq4aOACz79vFKYFNtn6h/dzBwroaBoZ Xa6x4CJfaa/k0GD5kQHDQyP4LQfoWhQ4mFPjhfWc1ORsyXCOupka51jg7VRxtimEgi NHB4RHm4fuMBg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9B71718006A for ; Sun, 17 May 2026 18:42:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) (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 ; Sun, 17 May 2026 18:42:45 +0000 (UTC) Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-365d8e43759so1076969a91.0 for ; Sun, 17 May 2026 11:42:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1779043359; cv=none; d=google.com; s=arc-20240605; b=B/tJ/XzeSzO1SUfxBoRTso/M2bct4D9qpEx9wBvQxQdQspZpJ4uEF85WNEwBu6b3v/ 0u86lQzoAU9wIpgiCbB1HI7B7DAlPieMzl0xg2eJcEHj14olvFMBAMYWQnZG++MmXaO1 bp4DrRxZenJ3qeLg5wwe66HVr1pdDJPRltlNlLeBpjkKxUfIWXGezpxEli4JiUIXv1Zu HMJEwVhBqrMCUyb5mUI1/W/6RYEYzjUlHLS/J2lPoPNwwFKkblSUEjkSlQoMpcIr4x51 daIQy2CyXC1NHHgKZq/sh09rfJXOfIfZC9aRCzaANLVONJ5M6ca1UuhKvtAAErTrlN+G mN9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=0wagC/vvdE2Orj+rN4Ld9P145HNTUZGjktNlqjFaw8s=; fh=Mtt/Kzl6bPOPRN0EKCjKypygfJHVzMorYNbfTSwkhDI=; b=FItqFWmOZYViI4q2eJAUU9SstdV0f6sILsZC9OVy2+EfpruYQcZUjCvv9tySu3QqcJ 6BmL3Z5tlidKp2s5fsTVuL4z9s7Sj4e7ALdpHtEsN4hZWFyQy/cVPqFW6kSCX6Llx6/H j+s2kAxlaQqNyAHFJDnmtUf2nVIM7FRfBa/OcVtHiLM6hqO5iv0wZtrPcvp0KpZHcsWW gqVbg3AYg8WI/TnSEpPABrBJQ9sY2OuXRW4cH+nmz1M9j877iMCOLwrFYg6wKx6y+wpu +gu4IFpp+dgg0jzzoKnBKO9Rws7ZyctyjBzMFSKKqvSMynaCssHfZ8cr06tuCmBIQHCX 1sQg==; darn=lists.php.net ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779043359; x=1779648159; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=0wagC/vvdE2Orj+rN4Ld9P145HNTUZGjktNlqjFaw8s=; b=cyXzm80Od5gc4cElOboMry5GQNKIaycvNekFyPGziXR5UAy8U3p5HzHgqGvEvFLNwb 3/l6KCgQgK6jTywQulxcg6KV3Yh6X7kxMIZ5bRPET8uyXe/rIcwHQiiNUOyoHfk45VQG BVvc/wAKebFsU2L1Ijzux6egV4fZVao8nALU1xUV/+9rsQnXMiBD2e8W2HXUzGy2EQrH d8IyxLXDIousN7aMCGlLXQoHOTKoQDdm/zm+tXVcD173buRx7Y+2FsBGvtoFClih2whJ iEAp7WjzgCDm9l6ZvQT0rNdQr225XhtmQz1iew7cXVVgwn/CUkjlM/lGLOhXYoJr7k/0 emXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779043359; x=1779648159; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=0wagC/vvdE2Orj+rN4Ld9P145HNTUZGjktNlqjFaw8s=; b=W6Ck9agYhd7Usk4cXIEQ+OlnNQCmtjkkiP67dSQSuas7TlFlwFK0N6CieoLc6BSuha SndzoB06/Ueb/p/Ap72YzhqMLjtzoaaBXsbeM3dG6PWUOHvFn9/yl0mTBbnbbuO4Hh7A JyQlrsOQyX5hw9gm3xxs8y4jqG9muwQK2JbbCH9kZ3lr25nyR3TtlkBu3djE4r+xc8da iedtyNHIc1x6D+7uxTn9P8iL4M+ELr12T+2mZKzXvhq8OmqZSjHZsJzSGXZvofPWi/Lb G8I8o+i+IwHQCP3MAPb+6GQ2gdTK2wvw/dEo/3wWXz35/E5kh5+tfVD72OR89fyvP7c7 T12Q== X-Gm-Message-State: AOJu0YxwnOSnz3JlRb+WKMpy9aMYFOCaPA0oEs3Vlgs19ceSo+rOh38L pLjPV9cq4+Zq4mDrd4MyxiUGCyYYuRnZKKhdxvDJ9HGQ15+6j6ClnBSp3SiYGIIXNm+mxpRlZ4c xZmSOGALffqcU7O/UcasHlcNEo0Heog== X-Gm-Gg: Acq92OFO44k4k6hR2wC8i2fpkKgG5HAhd90w8pi7OTmSy04zBY6o2xqNq0my5801hlW 0HZElH7MlYmbxFlvUPKjsZ/IpisEsxtN9okB9HIRWcPiYQdHLCXVipNkvNUexuJCvOBmFmG+R+j WAmPcCbrwL2hMNsCs49U5sSaPLGvPd5Om9O8XgyBOeB0moaF/9vWvDQSaSCKMDSl26BWdjwPscX 49dY34Jz4dTGAM9FEmF5gmNj9yfd+B905JsHoC3uS7UI8NFBtv/rVO+GqTvuN9qu9CGfM+Bp6WR 6y9tmdE= X-Received: by 2002:a17:90b:5628:b0:369:3c3d:f899 with SMTP id 98e67ed59e1d1-3695133ff6dmr9278407a91.2.1779043359205; Sun, 17 May 2026 11:42:39 -0700 (PDT) Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 References: <55c5833f-0fae-43ef-bd34-e7ae651ba5c7@gmail.com> In-Reply-To: <55c5833f-0fae-43ef-bd34-e7ae651ba5c7@gmail.com> Date: Sun, 17 May 2026 21:42:27 +0300 X-Gm-Features: AVHnY4KRFXpFy5JhgbtYdeBXhEJrKwHo4bZX2h_5PSaweHQbW36I4fMfds6R2pw Message-ID: Subject: Re: [PHP-DEV] Coalescing and nullsafe same time To: Vadim Dvorovenko Cc: PHP internals list Content-Type: multipart/alternative; boundary="000000000000773086065207cfab" From: udaltsov.valentin@gmail.com (Valentin Udaltsov) --000000000000773086065207cfab Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Vadim Dvorovenko : > Sorry for raising such an ancient topic. > > =D0=90ccidentally noticed, that construction like > `$arr['undefined']?->foo()->bar` causes error, and not behaves the same, > as `($arr['undefined'] ?? null)?->foo()->bar`. > > Readed docs, https://wiki.php.net/rfc/nullsafe_operator, asked google > and learned, that there were much discussions about such behavior, and > it was chosen deliberately. Nevertheless, structures like > `(($arr['undefined'] ?? null)?->foo()->bar)` does not looks pretty for > me. `$arr['undefined']->foo->bar ?? null` works great, until function > calls appears in chain, and `$arr['undefined']->foo()->bar ?? null` > breaks. It looks like objects are our favorites for short and safe > operations, but arrays are not. > > But during development, you can equally easily encounter typed arrays > (for example, with a structure described through phpdocs), and working > directly with properties, and traditional getters. An structured > associative array that has optional fields is also a common pattern. And > in large projects these patterns they can be found in almost any > combination. > > So it looks like the language needs a construct for pretty code in such > cases. What do you think it should look like? > > * Make nullsafe operator to be coalescing too. Making > `$arr['undefined']?->foo()->bar` no to cause error, but return null > > * Some new coalescing-nullsafe-operator, `??->`. > `$arr['undefined']??->foo()->bar` suppressing all errors to the left > same way `??` does. > > * Some new nullsafe-array-access operator `$arr?-['key']`. This leads to > `$arr?-['key']->foo()->bar` for cases where we are sure that array has > key. Supposed short-circuiting working here too. > > * Some new coalesce-array-access operators `$arr??-['key']`. Making > `$arr??-['key']->foo()->bar` for cases, there we are not sure, that > array has key. > > * Suppress function call on nulls and undefined errors in > `$var->foo()->bar() ?? null`. > Hi, Vadim! I've seen discussions about that and also considered proposing something similar, so thank you for bringing that up! I see two questions here: 1. Accessing an array offset on a nullable value ($arrayOrNull['offset']) 2. Treating a non-existent array offset as null ($objects[0]?->prop) For the first case, the syntax could be symmetric to ?->: ? + -> =3D ?-> ? + [$offset] =3D ?[$offset] $arrayOrNull?['offset'] , equivalent to $arrayOrNull =3D=3D=3D null ? null = : $arrayOrNull['offset'] For the second case: $objects[?0]?->prop , equivalent to ($objects[0] ?? null)?->prop -- Valentin --000000000000773086065207cfab Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Vadim Dvorovenko <vadim.dvorovenko@gmail.com>:
Sorry for raising such an ancient topic.

=D0=90ccidentally noticed, that construction like
`$arr['undefined']?->foo()->bar` causes error, and not behave= s the same,
as `($arr['undefined'] ?? null)?->foo()->bar`.

Readed docs, https://wiki.php.net/rfc/nullsafe_operator, asked google
and learned, that there were much discussions about such behavior, and
it was chosen deliberately. Nevertheless, structures like
`(($arr['undefined'] ?? null)?->foo()->bar)` does not looks p= retty for
me. `$arr['undefined']->foo->bar ?? null` works great, until = function
calls appears in chain, and `$arr['undefined']->foo()->bar ??= null`
breaks. It looks like objects are our favorites for short and safe
operations, but arrays are not.

But during development, you can equally easily encounter typed arrays
(for example, with a structure described through phpdocs), and working
directly with properties, and traditional getters. An structured
associative array that has optional fields is also a common pattern. And in large projects these patterns they can be found in almost any
combination.

So it looks like the language needs a construct for pretty code in such cases. What do you think it should look like?

* Make nullsafe operator to be coalescing too. Making
`$arr['undefined']?->foo()->bar` no to cause error, but retur= n null

* Some new coalescing-nullsafe-operator, `??->`.
`$arr['undefined']??->foo()->bar` suppressing all errors to t= he left
same way `??` does.

* Some new nullsafe-array-access operator `$arr?-['key']`. This lea= ds to
`$arr?-['key']->foo()->bar` for cases where we are sure that = array has
key. Supposed short-circuiting working here too.

* Some new coalesce-array-access operators `$arr??-['key']`. Making=
`$arr??-['key']->foo()->bar` for cases, there we are not sure= , that
array has key.

* Suppress function call=C2=A0on nulls and undefined=C2=A0errors in
`$var->foo()->bar() ?? null`.
<= /div> --000000000000773086065207cfab--