Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128973 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 9AC041A00BC for ; Mon, 27 Oct 2025 08:03:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1761552209; bh=DyiDpaiO1jdxpY1Y2j4AIur7fPy/94k0JjkLMwOwT9c=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=OyG0PFftFxnHFrzsCJ6lb7LIeunEaN/p1XiqGTqBYsA/Kg3JNrISCuV9ZupqfzhOd /rC6GZAsmijYJTlKh0tz3nmhSCLE69xUHqhMEWQVHvoVqvSHH7QX5wDmiVIa63vXMY ROoIhnfPYvXg88gpn9HghRe2xepk2a4LDqacekdO2dvSP++WCOPo7D9DafTTxnyzdq czX0K9jKil/u4SVAuUXnwZnk8K0lCO4GfObMcRQJt9MCJmry58MMODp0z/OIiGxn69 vVDBq92WZsPFVCpM52DbhsCHs6FxQ9s6YgFzEUiOBmMzgXtzKl1vAVykhG/3tolReT 4ArJA8QAVjmCQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5863518003E for ; Mon, 27 Oct 2025 08:03:25 +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.2 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,T_SPF_TEMPERROR autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) (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 ; Mon, 27 Oct 2025 08:03:25 +0000 (UTC) Received: by mail-ed1-f50.google.com with SMTP id 4fb4d7f45d1cf-63c12ff0c5eso8567546a12.0 for ; Mon, 27 Oct 2025 01:03:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761552199; x=1762156999; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=DyiDpaiO1jdxpY1Y2j4AIur7fPy/94k0JjkLMwOwT9c=; b=aQLO1rdgE1N6e5I3ntfOjdBeurk5TZiBOb3DtrxFUPuXQ5ncXJ53FTAc4p22Va+24d QXu6LnBdvu/9/u2LBZomHo4YGIvPNVTg0L849BHRzcTzkAZpxkuAibutmaWWrf3vUc+t WEX/EALII/A0cVK49uCwYqbV6RaZBJOy0YBnbQ2KdrEjU1Dsm14GyPHRV0sBbAgV56u0 3yBv0sG3Vatxe83ruhRFcfelijc5K33EqhMRAM8czU7CgOfPPC1jhnwMWbQPJn02SI+Z ad4Gcm+oCM0G0MugBypqyYSFwvJbFTNxEiV/kkoMk2QHkQf6qhZNSobAS6J3wRGhufUB 5ucQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761552199; x=1762156999; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DyiDpaiO1jdxpY1Y2j4AIur7fPy/94k0JjkLMwOwT9c=; b=H4+c/Usn7neYwNiP1Djpw3fPeb58s3wT/kI6hue8/4pGXEje4q6IAocGnGMhzebP9o zky2zHBFYZc7f4ORiEeELhrm29Ecfy5Vs8q+OLqSyC2vmXZ/t8w2zKDYEzr9PQDO+R1l Tu2jm8AqwDQ7vj8bKcaeG0t/hmBUC237klbFzhi0MU0ZDyJpeKRLk4pe/v+cbhUVix1H fJ1DUlfn37qVX473xOqwlwbg1ihcdin9hqJ1kriR4cumvbtk7RmefwXlIn/vYa46iLsZ 7kpszTsPdftzZBAmA0E0mW315UzD5KguvfLSBwEMJ3gfZc81ZJh1mbLtXwFPCpFa3MJ/ Qk5w== X-Gm-Message-State: AOJu0Ywmcs9VhxL5MK3W84ztJ1pKy9oouXYXMrzWP4FEZU/mdT4RcBkZ nSQkPOjb1mo/rX8/DOMiDAEsQVAkD+8Opolb+xgqoqRKfUJugeGFwXS0hXmpHwuXV2NBpg7Vz1l P/NZFfZ3oGciTOcGjLCaLRbCaBMF8EDExGqoc X-Gm-Gg: ASbGnctgxwSbMzq20p7McdcEeK69myzP+Pjjo/lk1004ZnArwbwunm7cCHb/+ZBwsbT LsFe1xtxDo//yjCLYpv0U0rFDEHVBMbDkUaHOyacGxydH81wtY58P0+08X8eR35pqsYx8fS4gAI brIweSW2yl7yTQekk2K07mnqkLs3PqZsV9s1qMgD6yXUiuLKPs3G62qpatQrIOa/q7EFa9Qy6qv rPqV2ilrMCoh6EH3w7z6MiGR1jX882mhUvlEIZmEW6z8auoI2qVqDH8rOoJtVxQKsOcW+HhBcM+ TG4= X-Google-Smtp-Source: AGHT+IH+9g1W5b7Y/X8pJ0VL+s9JFvymtjq9mxN7V/T0u/I1277fVByakMSUR3chSg30Aq8QAqvL2JcpS6b7HklCHhA= X-Received: by 2002:a50:9ee4:0:b0:633:14bb:dcb1 with SMTP id 4fb4d7f45d1cf-63e5eb3e091mr6358301a12.11.1761552198386; Mon, 27 Oct 2025 01:03:18 -0700 (PDT) Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 27 Oct 2025 09:03:06 +0100 X-Gm-Features: AWmQ_bk6UZIycwYghzH-xzAY7mwJsudkM5AzaynHG4zZhlqi6Bnx0X-6S4HWIWo Message-ID: Subject: Re: [PHP-DEV] [RFC] Nullable and non-nullable cast operators To: "Rowan Tommins [IMSoP]" Cc: PHP internals list Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: alex.daubois+php@gmail.com (Alexandre Daubois) Hi Rowan, > PHP definitely needs a better range of cast operators, and I've been thin= king for a while about what that should look like. My main concern with thi= s proposal is that it picks a few specific options and uses syntax in a way= that's not easy to extend. Thank you for your interest! I get your concerns about the extension of such syntax, but we don't feel that there would be a particular need of extending this syntax. The syntax does not bring something really new: the behavior described in the RFC already exists in PHP =E2=80= =94 we are just making it available at the cast level with explicit null handling. > What I mean by that is that there are, roughly, three things that a user = might want to specify: > > 1) What *output types* are desired? > 2) What *input values* are considered castable? > 3) What should happen for *invalid* values? > > For (1), our current casts allow base scalar types + array. I can definit= ely see value in allowing nullable types there, but also other complex type= s, e.g. (string|Widget)$foo could be equivalent to $foo instanceof Widget ?= $foo : (string)$foo > > For (2) and (3), you could say our current casts accept anything, but you= could argue that they replace invalid values with a fixed "empty" value fr= om the target type. > > More importantly, there are a few different things you might *want* to ha= ppen: > > - throw an exception, because you expect the cast to succeed, and want to= abort if it doesn't > - fill with null, or with some default value from the target type, e.g. b= ecause you're *sanitising* untrusted data and want to proceed with the best= you can get > - detect that it *would* fail, e.g. because you're *validating* data. > > Throwing exceptions is generally a poor choice for validation or sanitisa= tion, because you have to either make the user fix one mistake at a time, o= r add a separate try-catch around every check. > > The current RFC adds support for nullable types (1), but only if you *als= o* want a different set of validation rules (2) *and* a different behaviour= if those rules aren't met (3). That's a *useful* combination, but it's not= the *only* useful combination. Using the (?type) syntax for that combinati= on makes it hard to add other combinations in future. I'm sorry, I think I don't get what you mean. It looks really complex for something that we would like to keep straightforward. This RFC doesn't really introduce *new * behavior: these rules already exist in the engine. Casting with these new operators is indeed stricter than existing ones and it _may_ look it introduces brand new rules, but it actually isn't. This strictness comes from implicit casts already done at function calls. =E2=80=94 Alexandre Daubois