Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104475 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 16681 invoked from network); 19 Feb 2019 19:48:35 -0000 Received: from unknown (HELO out2-smtp.messagingengine.com) (66.111.4.26) by pb1.pair.com with SMTP; 19 Feb 2019 19:48:35 -0000 Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 6793D22078 for ; Tue, 19 Feb 2019 11:33:15 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Tue, 19 Feb 2019 11:33:15 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=Qrmvbc wozb1lzkr+3dF7Wp/pYrtPkLMWM/bvWXFCWLM=; b=By9ibozeOjtPD352mBz3y5 FwYOhBbF1qt+xkIJ4xtQFDq4BT4+xuvMSjmO+M8gGhcIg+LIgtfCTKZVGS9eD5ub vqxt/TJGRSmv/aH3GePFqEVkHBPV1cpfnqYcJNY3JkKaEYHQzxFkD/9+vwmhOQwx I/nq4V36vYzOqpmxRN7Zi29NigFGUQBy2zSp4trhSaeucFd8Ajf5wQnglIVG5sGQ RFPYO6fymCxJbCm14LD0p0T40YTlbQ3m84aOu85yCOfTh2nkNlPipaajVafM5yCT pzs+1v0ADDnHEWMSEtyhayrbvjuC6QEjmYWBCodf1hPR0nq2pnGDh9thUkt1ec1w == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedutddrtdeggdekkeculddtuddrgedtledrtddtmd cutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfhuthen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkjghfgggtsehgtderre dttddvnecuhfhrohhmpefnrghrrhihucfirghrfhhivghlugcuoehlrghrrhihsehgrghr fhhivghlughtvggthhdrtghomheqnecukfhppedvudeirdektddrfedtrdduhedvnecurf grrhgrmhepmhgrihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtgho mhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from vulcan.localnet (216-80-30-152.s3222.c3-0.frg-cbr1.chi-frg.il.cable.rcncustomer.com [216.80.30.152]) by mail.messagingengine.com (Postfix) with ESMTPA id B8E2C10311 for ; Tue, 19 Feb 2019 11:33:14 -0500 (EST) To: internals@lists.php.net Date: Tue, 19 Feb 2019 10:32:31 -0600 Message-ID: <5767621.reugzEP4TJ@vulcan> In-Reply-To: References: <3247148.HBVCQ37Dhl@vulcan> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart7834614.8o7ypyyPOR"; micalg="pgp-sha512"; protocol="application/pgp-signature" Subject: Re: [PHP-DEV] Allows arrays to be type-hinted with interfaces mimicking Array behaviour From: larry@garfieldtech.com (Larry Garfield) --nextPart7834614.8o7ypyyPOR Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" On Tuesday, February 19, 2019 3:38:16 AM CST Rowan Collins wrote: > On Tue, 19 Feb 2019 at 02:44, Larry Garfield wrote: > > Yes, a lot of existing code uses arrays as anonymous structs. Such code > > is, > > IMO, "doing it wrong". That includes PHP default behavior in many cases. > > The > > super-globals are one of the exceptions, as since by definition you don't > > know > > what the keys could be in advance you need some sort of generic accessor. > > > > superglobals anyway on the grounds that globals are evil (fact check: > > true). > > There is often an object wrapping around the session that has get() and > > set() > > object be kind-of-array-ish-for-some-part-of-array-ish-ness is a > > long-standing > > problem, no question. iterable was a good addition. I think there was > > discussion of a countable type hint at one point, but I don't think it > > made it > > That leaves ArrayAccessible (aka, you can put [] after it and something > > useful > > happens), which is what you describe. I would not be against adding such > > a > > instead of an object is Just Plain Wrong(tm). Even a bare public property > > class is superior in every possible way, before we even get into questions > > of > > useful accessor methods. > > > > be. (Eg, parsing JSON, GET or POST parameters, etc.) > > 2) You're being sloppy and not thinking through your API. > > struct into an object that has appropriate accessors that can then handle > > error cases, missing values, etc. Not doing so results in things like, > > say, > > particular anonymous-struct was missing a certain key, which I didn't know > > was > > possible, but it broke the whole application. (Seriously, that was my > > entire > > struct, even if you're using __get() or ArrayAccerss to do it. (SimpleXML > > is > > an example of that approach, for better or worse.) > > where you have to care, but those are edge-casey. > > > > ArrayAccessible is a code smell, and the solution is to properly structure > > your data and type hint on a defined class instead. Including an > > ArrayAccessible pseudo-type would be just a matter of completeness, not > > something I would advise anyone actually use. > > Hi Larry, > > The above post seems to have been mangled somewhere and lost some of its > text. It's mostly readable anyway (and I think I agree with what's there) > but if you have a copy of the original anywhere, you might want to re-post > it. > > Regards, No, I don't. That's the second time KMail has eaten half a message on me. I am now officially in the market for a new local mail client, because that's JUST NOT OK, KMAIL! The gist is that if you're dealing with unknown foreign data (JSON, GET/POST, etc.) you should be wrapping the raw structure into an object that provides a better interface, default value handling, error handling, etc. If you know what the keys are in advance, make it an explicit class, period. So while I would be OK with an ArrayAccessible type hint for completeness, I would view using it as a code smell. Insert debate about union types here, as it's bound to come up at this point. Excuse me while I go find a new Linux mail client that isn't a buggy data- losing . --Larry Garfield --nextPart7834614.8o7ypyyPOR Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part. Content-Transfer-Encoding: 7Bit -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEE/ph/GXfY8v0YwFBp4MMKwDFxYXcFAlxsL58ACgkQ4MMKwDFx YXeIPgf8D009pfs8PW4rhOyYazqfWtw3n/AOXacFCHFsTGXk5WvwvS4sAeTkhrPX HBWPG93Ze5//e7HkyQp018dzmPVFqC5nfAA45tYz0uhq5OpB1HSl6GRwhjSABtlv Q8LyR2RZmyA2tpBiHp00z40UUksE//Xf0q2gdz9RSFEvxLdrB8Y7zXB6FNw9Krym zfOXZ758wABLqE5Spt1USqfmbyoJJ+WwJgx9Ld8A6f3psvZkOUlWx+HAURMpWAtb vqx1ndr5q6Orki6z/Q9I/oehZZJBbHVsbP5W7M6HUhrRDB8QO2CnszqmLx89b2wP Z+fUBKLRXcK/OqDW5w8e0umFsSvIpg== =y4Au -----END PGP SIGNATURE----- --nextPart7834614.8o7ypyyPOR--