Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104460 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 86549 invoked from network); 18 Feb 2019 19:42:33 -0000 Received: from unknown (HELO out2-smtp.messagingengine.com) (66.111.4.26) by pb1.pair.com with SMTP; 18 Feb 2019 19:42:33 -0000 Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id C383921AB4 for ; Mon, 18 Feb 2019 11:26:58 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Mon, 18 Feb 2019 11:26:58 -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=gJ3z7G n0ljTlNkJyQaMqX7mQYo4ZKVTVWTkkK+EepFc=; b=UHygW7QH/dGUje6cmIpRcY pIp8J0cNP2DvLgJBwaHWaJA4SYd44tGRlC7+TFZTjerEkVh8pGPdsmIGucSH7REj 0exdUyx5T/YwYWiaT6WkQBGvz4rKN8YNpc7Yc7HeIAVllbxGuDnhlSw+hjaZcK1g zwzASQ3PUAFbYLhcfK1SPH1QWAIIOBNmlia/DHoYjEH+d7hrZ8jga/xIc8d2ynDa 0cS7IPHPh8Yv/pK7LZoNiyvnYAE20zG9A+1eJ5CJ8YzzhFj0vR7NFO5hS4ymX5o8 2o4/WchI+NfV9PTeIv9j2GzsWABlmDWMqC6yBuhO0T7TKkuSQbzJyq8ggTV/3mgQ == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedutddrtddugdehjeculddtuddrgedtledrtddtmd cutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfhuthen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkjghfgggtsehgtderre dttddvnecuhfhrohhmpefnrghrrhihucfirghrfhhivghlugcuoehlrghrrhihsehgrghr fhhivghlughtvggthhdrtghomheqnecuffhomhgrihhnpehsthgvvghmihhtrdgtohhmne cukfhppedvudeirdektddrfedtrdduhedvnecurfgrrhgrmhepmhgrihhlfhhrohhmpehl rghrrhihsehgrghrfhhivghlughtvggthhdrtghomhenucevlhhushhtvghrufhiiigvpe dt 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 97D65E41AC for ; Mon, 18 Feb 2019 11:26:57 -0500 (EST) To: internals@lists.php.net Date: Mon, 18 Feb 2019 10:26:56 -0600 Message-ID: <1838251.7kDW7efa0E@vulcan> In-Reply-To: References: MIME-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart1688665.0M6USnNuZL"; 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) --nextPart1688665.0M6USnNuZL Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" On Sunday, February 17, 2019 9:24:05 AM CST Girgias wrote: > Greetings internals, > > I would like to have your opinion on being able to type hint arrays and > objects which implement > array-like interfaces (notably ArrayAccess, Countable, and Iterator) as > currently if a function /method only needs one specific feature of an array > and is willing to accept array-like objects. > There are some rather repetitive boilerplate and the impossibility of using > type hints. > > An example of such a function, (where $arrayLike could be custom session > object) : > > /** ArrayAccess|array $arrayLike */ > public function checkCSRF($session) { > if (!is_array($session) && !($session instanceof ArrayAccess)) { > throw new Exception(); > } > // Check token > if (!isset($session['CSRF']) { > throw new Exception(); > } > // Do some more stuff ... > } > > As it can be seen this function/method doesn't need any of the other > properties of an array > such as Countability or Traversability to do its job. > However, in the current state of PHP, it is impossible to accept array-like > objects without > writing the initial if statement. While I would like to see better consistency between arrays and array-ish objects in principle, in this case I think it's the wrong approach. You're using an associative array as a cheap anonymous struct. Don't do that. It's far less self-documenting, far less type safe, far more error prone, and far less performant. Just switching $session from an array-ish value to a class with public properties would make it use half as much memory. cf: https://steemit.com/php/@crell/php-use-associative-arrays-basically-never https://steemit.com/php/@crell/php-never-type-hint-on-arrays Making it easier to use anonymous-array-or-object-acting-like-anonymous-array would be a step backwards in every possible way. --Larry Garfield --nextPart1688665.0M6USnNuZL 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/GXfY8v0YwFBp4MMKwDFxYXcFAlxq3NAACgkQ4MMKwDFx YXfvqggAhdNvqiHfiER0VnFl2m6xSWZfgjPt6driYRrx74yaJSkmTsHFAFJ0czuy Qp5+R78YBynH8Q1id8xdjiZqqw34u9ktTjDyy+c1olyvzkLt1IYvAdF8tg74DvUV v+WB/UeHVyRWqa2f64xqpN9oxeFwX2m+4igDCqwGKxBTRqQRvP8s4DcOqwDTNaBX N1X6Mh1Bn2/WA3CXVVCTVklnWsK+K1RKp55GFTeWHLTn6l0KIcPWaybXm/g78zMy Y/9dvF/E5HQuySMj+jxpOnb3xy11Pd+T4jVSeonEel2ipa8Y5FuQOfZgWxE9978w FscplCG+ospd5R964sx0TrA5cmPrtw== =KVP9 -----END PGP SIGNATURE----- --nextPart1688665.0M6USnNuZL--