Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126532 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 3C9141ADCBC for ; Fri, 28 Feb 2025 23:29:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1740785201; bh=F058Kpf+puXtuuj23EhimkifFDUbTgGwef3W8C1Ht68=; h=Date:From:To:In-Reply-To:References:Subject:From; b=CjpmfkBELcC7GaSb0vUt9JzoifA7wbU+cHSL4c+pgd6CYD2f/UcZQ7YQRls7b+pGJ oWYAP5xZUSXDOjofs3noeGW+Hy83enGxnP5gDWfV6J37BxP1fuOBnLaDsKBGlTtqRG jHyvIknbBXia0wHcaXXZ5R9gb43ZEnuIbo9gcFV5+NtQl83eECOiUofXJkbTPF68sr JAqVPM01dYdXFhx5N7a35tG6UOaEWZ2AHfnPZZePEr2gKYnqFWR36d6qaxSgHwCRDR lryo87Lvcb14wWWRL9kXH4iQXL/OeUp4jUCKmjKQIwZCR4ZZ/pssnD9XuvLSigouJ5 UWtkzjByblFGA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8D7D51804E1 for ; Fri, 28 Feb 2025 23:26:39 +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=-1.4 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fout-a5-smtp.messagingengine.com (fout-a5-smtp.messagingengine.com [103.168.172.148]) (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 ; Fri, 28 Feb 2025 23:26:37 +0000 (UTC) Received: from phl-compute-04.internal (phl-compute-04.phl.internal [10.202.2.44]) by mailfout.phl.internal (Postfix) with ESMTP id 639AD138113F for ; Fri, 28 Feb 2025 18:29:14 -0500 (EST) Received: from phl-imap-06 ([10.202.2.83]) by phl-compute-04.internal (MEProxy); Fri, 28 Feb 2025 18:29:14 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to; s=fm1; t=1740785354; x=1740871754; bh=LYMaBzGYFDNJ+UdNz0CG3 3/bF5b0wny3hK0/gbvRdUc=; b=Lw2FBP9fDwRgTPF24Vr8h1cz8YkhAzTpkF2SU DenFniF1grrNNxX3YZrRkt1zxyeCJUiKURWjoVyVSvyvmF1rtlWZ/0zvRriL265Y 6ia6LNiHRq6SkaADr6+TmJ8uC2M6DtHQgUr4wD2fbtJrs3NFQyj+HlsrR+EHClOR nIJmKqK4fQuMqic61Tf/h95BsQ4ZMiZqPjoDYn0OhOUttd7i33aAo3He9wwr8TCb hrzONXCZkFdVMOpOjEQCpIdxOvhHBGEDXqBVN4S0Pxuw/TnG0N5E+v4FGZuIQ1I8 xEstAtAx7beRaEcNUitM6R4cHOTJLYWMF+r6N3Iex73t7Sdig== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1740785354; x=1740871754; bh=L YMaBzGYFDNJ+UdNz0CG33/bF5b0wny3hK0/gbvRdUc=; b=W8Z+7o9DdmrPeRXLy uKOTV7pHt0OTTcZtMfZlZNM525NekU6FCui1+kAeXRsZ4Ny4IiqiUaRAHMjzjGG3 /iuBCQ4zv804EwsQBiKfzUzSR4Nn1eCEGDXvr0OsmSesL0nH7BcdfBHx8XiSWrHt qY52vtU7S+XHnp+yGFbp3CT8Qdu0ngjkveGS1jHB7q+1B3GUNS/uH7KwHPKFvUrU vUAZBlxE65IaLffTpY5a2JjKV3z3YvnbM256rnE2urlbuVCGG5J3+8A2KO7igQnw gUBOQ8wGVvITa/qY5/EG06wuAywa0iGqkFX7CoOQ3X5Hu+HFq+GBq12dLuXEBYJ6 dlkQQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdeludejvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefoggffhffvkfgjfhfutgfgsehtqhertdertdej necuhfhrohhmpedfnfgrrhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfh hivghlughtvggthhdrtghomheqnecuggftrfgrthhtvghrnhepteejveehleetheeutdeh heelffeludeiteefffdvueduleegtdfggfegjeektddvnecuffhomhgrihhnpeihohhuth husggvrdgtohhmpdhphhhprdhnvghtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghr rghmpehmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhmpd hnsggprhgtphhtthhopedupdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehinhht vghrnhgrlhhssehlihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 0093C29C006F; Fri, 28 Feb 2025 18:29:13 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Fri, 28 Feb 2025 17:28:53 -0600 To: "php internals" Message-ID: <86f10d5a-c7d0-4592-900f-fdcd11b8ff3c@app.fastmail.com> In-Reply-To: <81F87FFD-91EF-4FDF-A929-9BE1CA08AA1E@garsi.de> References: <81F87FFD-91EF-4FDF-A929-9BE1CA08AA1E@garsi.de> Subject: Re: [PHP-DEV] Vibe check: array shapes Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: larry@garfieldtech.com ("Larry Garfield") On Fri, Feb 28, 2025, at 4:52 PM, Alwin Garside wrote: > Hello, > > Long time listener, first time caller here. > > Last week I had a shower thought about how it would be neat if we coul= d=20 > declare array shapes in PHP, and use them to provide structural=20 > interfaces for arrays. > > After all, arrays are one of PHPs strongest features, and anyone who=20 > has ever tried to use arrays/maps in other languages after getting use= d=20 > to the versatility of PHP arrays will have to admit that nothing comes=20 > close to their flexibility and ease of use. > > However, ironically, most of my friends, colleagues and fellow PHP dev= s=20 > usually tend to shun PHP arrays, which has led to the widespread use o= f=20 > Collection classes and the like. The usual argument is that arrays=20 > don=E2=80=99t provide any way to declare their structure, and thus don= =E2=80=99t=20 > provide the ergonomics of autocompletion (even if you declare the arra= y=20 > shapes with docblock annotations IDE support is iffy at best) and don=E2= =80=99t=20 > provide any form of runtime type checking. Your friends are correct. Arrays in PHP are an awful thing. They are t= wice as memory hungry as objects, they provide no static guarantees, and= they're a security hole waiting to happen (and has in fact happened). And since PHP 8.0, *we have something equivalent to array shapes already= that is vastly more efficient*: Constructor promotion. // This tells me nothing. $arrayPoint =3D ['x' =3D> 1, 'y' =3D> 2]; // This tells me everything. class Point { public function __construct( public int $x, public int $y, ) {} } $objectPoint =3D new Point(x: 1, y: 2); $objectPoint will use half as much memory as $arrayPoint. (Not meaningf= ul in something this small, but if you have 100 of them...) It provides= all the static type validation you can get in PHP right now. It's self= -documenting. It works with everything else in the language. And it's = already been available for 5 years. And now you can also toss readonly,= private(set), or hooks around on it if you feel like it. This is a solved problem, and the solution is objects. Using arrays as = pseudo-objects is the wrong approach 99.9% of the time. Collections are= a different matter, as those are real lists. For more details: https://www.youtube.com/watch?v=3DnNtulOOZ0GY All that said! A side effect of the pattern matching RFC (which we'll be formally propo= sing real-soon-now-I-swear) is that it effectively gives an array-shape = validation check for free. https://wiki.php.net/rfc/pattern-matching So yeah, no thank you on a separate array shapes RFC. --Larry Garfield