Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126321 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 qa.php.net (Postfix) with ESMTPS id 0CD3D1A00C3 for ; Fri, 7 Feb 2025 07:38:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1738913733; bh=xLEiFmlbnnk9CkbVtKHQ+aB9brW6q/vDq/6otRuKJ20=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=h7Gh+e9IYDoauqb3cVzUVM19YC6on150t3v8SBTipV9bdfAqO/O6AxDYN/xSSG5PZ 0t7jwzZagDdpf4mJchFO5DC8Oc0UteoijJ7KxyD8wklzWICPbU4x/Gep8slt2E/FUT 4MZiRJCouqQ6uG5huJFfIOt6uUbhNUDXZk3g6QyLvNFXczrkG49ucyIByaViYJxlYr Uc/8woaYofykYpA15CiE5qXxQFESK2/onwJ0HD11v5uLddjD7dUvfkHSSS4FoXvtAg VvTZKuVT/cdyfFa8jMVwXAhWBtRR8FKfJcan12wADfrsK77Ld86wtyzEVDYIndKmk1 39tk2kuatLQyA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8C8DD1805E6 for ; Fri, 7 Feb 2025 07:35:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_05, DKIM_INVALID,DKIM_SIGNED,DMARC_MISSING,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,URIBL_SBL_A autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from sender4-of-o54.zoho.com (sender4-of-o54.zoho.com [136.143.188.54]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 7 Feb 2025 07:35:28 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; t=1738913891; cv=none; d=zohomail.com; s=zohoarc; b=KqzDpvxjmljvOY/O4kRkXOcx4iUn0jB7+0tcHG8NZuGvxtYyUN1rMMGpjHT7ShphDkK7KrZJ7q8rWNHnGjwzLTkRBEJ+e/bsFtk3XuYCiaIUlKz5ptG9eHzg50N0+swZzeRM5zvSBkf5JNmGIAVjlORurTJFYRmPQe7KDGxNwIo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1738913891; h=Content-Type:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=TfUeOxQT2GjBBwRFimjs3FmiwdFksocq3c+AVGPP3C4=; b=cGwQb0w1VszB/+0Sk7TBQ7GQeGSYYp3tKuBRsHd/jLD5D6Qd6gz0d5j8195welIOPZ78+aSMNpDySNcNb4JOJk9rafWB46nmOFyklcXSAUi221tz6UjmxmetqG4X/ZAPlgTZwDU8Gvjo2FypZKgoRbUz4ZWrpPHxPkRz+f9F4TI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=faizanakram.me; spf=pass smtp.mailfrom=hello@faizanakram.me; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1738913891; s=zoho; d=faizanakram.me; i=hello@faizanakram.me; h=MIME-Version:References:In-Reply-To:From:From:Date:Date:Message-ID:Subject:Subject:To:To:Cc:Cc:Content-Type:Message-Id:Reply-To; bh=TfUeOxQT2GjBBwRFimjs3FmiwdFksocq3c+AVGPP3C4=; b=mS+Vp6tQDcGmqm+COTYXes8jaAJ6sPuozNI4LGnPjmjemLDHmqu98Pt9B8YJiiBQ uvNNZY4n5cuRonzoyMenbvKqjwlrennHBNFwkEVgduKugxbgDj72Fw6l6m8BSOiNinq okqEwHowDjoCFR56I4PoibBnCf8nwWMyFA1Dod60= Received: by mx.zohomail.com with SMTPS id 1738913889114745.5509766218223; Thu, 6 Feb 2025 23:38:09 -0800 (PST) Received: by mail-yb1-f176.google.com with SMTP id 3f1490d57ef6-e587cca1e47so1603379276.0 for ; Thu, 06 Feb 2025 23:38:09 -0800 (PST) X-Gm-Message-State: AOJu0YxvmrJiGjkwLZeG6RK49U3/ONSI8kijg6y4UfUsyH0hQysXRwiF VWlRlOqQf8YXHh0LxM/+YqmQlhz0rVlMUycqQDr60gvHJ9k0JmiuCC5q4w2+ZAagholkqDca9Lr REqzqKKcV4JraJmFtSdqF4vWpFtg= X-Google-Smtp-Source: AGHT+IFcM8lc+gfaR6VDKCXexbf1go8AMlU1954EwTHNmY9XGdZkJBtB0qc3fc+5EDbnMlOtYsP1fC2OVn5DYANtk3w= X-Received: by 2002:a05:6902:988:b0:e58:98e2:e447 with SMTP id 3f1490d57ef6-e5b4629dbe7mr1727357276.48.1738913888512; Thu, 06 Feb 2025 23:38:08 -0800 (PST) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 7 Feb 2025 08:37:57 +0100 X-Gmail-Original-Message-ID: X-Gm-Features: AWEUYZlCP-zEAeqroMY_mZxBxMAFzb6XiRRAAR685rdMrGMcLQXTHTJA8wYRQmo Message-ID: Subject: Re: [PHP-DEV] RFC: Not Null Assertion Operator To: Mihail Liahimov <91liahim@gmail.com> Cc: PHP internals Content-Type: multipart/alternative; boundary="0000000000009eba98062d8870c5" X-ZohoMailClient: External From: hello@faizanakram.me (Faizan Akram Dar) --0000000000009eba98062d8870c5 Content-Type: text/plain; charset="UTF-8" On Fri, 7 Feb 2025, 08:30 Mihail Liahimov, <91liahim@gmail.com> wrote: > Thank you for your answer. Now I will give examples for better > understanding. > > Simple examples from Typescript: > > let foo = ... > foo!.bar() > foo!.someProperty.baz() > > Examples of potentially using in PHP: > Without this operator we writing this code: > > $foo = ... > > if ($foo === null) { > throw new FooIsNullException(); > } > > $foo->bar. > > With this operator: > > $foo!->bar > $foo!->someProperty->method(); > $foo!->someProperty->anotherProperty!->method(); > > I think the postfix operator would be illogical in PHP because my operator > is similar to the existing nullsafe operator in syntax. And it would be > strange if its syntax were different. > Or we can implement both operator syntaxes: prefix for accessing > properties, and postfix for use with variables, as in your example. > > Nullsafe: > $foo?->bar; > Not null assertion: > $foo!->bar; > > If variable bar would be null - php will throw an exception. But now i > dont know which exception it would be :) > > This operator will be useful in cases where a null value in a specific > place will violate the domain logic. Usually we write either assertions or > checks and throw our exceptions for this. But it seems to me that the not > null assertion operator will help avoid writing unnecessary code. The > problem, of course, will be catching errors. It is not clear how to catch > errors by a specific value. I will think about it. > Hi, I don't see the point of this operator, php doesn't allow any operation over null. See https://3v4l.org/UJ5eg It's throwing warning (mostly for backwards compatibility), I'd rather have it throw error in next php version. Static analysis already complain about it, so whats the point of !-> operator? https://phpstan.org/r/c9d6e7b3-ac66-4e91-81e8-af0cafdc976c --0000000000009eba98062d8870c5 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Fri, 7 Feb 2025, 08:30 Mihail= Liahimov, <91liahim@gmail.com= > wrote:
Thank = you for your answer. Now I will give examples for better understanding.
=
Simple examples from Typescript:

let foo =3D ...
foo!.bar()foo!.someProperty.baz()

Examples of potentially using in PHP:
W= ithout this operator we writing this code:

$foo =3D ...

if ($= foo =3D=3D=3D null) {
=C2=A0 =C2=A0 throw new FooIsNullException();
}=

$foo->bar.

With this operator:

$foo!->bar
$f= oo!->someProperty->method();
$foo!->someProperty->anotherPro= perty!->method();

I think the postfix operator would be illogical= in PHP because my operator is similar to the existing nullsafe operator in= syntax. And it would be strange if its syntax were different.
Or we can= implement both operator syntaxes: prefix for accessing properties, and pos= tfix for use with variables, as in your example.

Nullsafe:
$foo?-= >bar;
Not null assertion:
$foo!->bar;

If variable bar wo= uld be null - php will throw an exception. But now i dont know which except= ion it would be :)

This operator will be useful in cases where a nul= l value in a specific place will violate the domain logic. Usually we write= either assertions or checks and throw our exceptions for this. But it seem= s to me that the not null assertion operator will help avoid writing unnece= ssary code. The problem, of course, will be catching errors. It is not clea= r how to catch errors by a specific value. I will think about it.
=


=
Hi,

I don't see the point of this operator, php doesn't allow any o= peration over null.=C2=A0

See=C2=A0https://3v4l.org/UJ5eg

It's throwing warning = (mostly for backwards compatibility), I'd rather have it throw error in= next php version.=C2=A0

Static analysis already complain about it, so whats the point of !-> op= erator?


--0000000000009eba98062d8870c5--