Re: [RFC][DISCUSSION] Collection Inspection
| From: | Derick Rethans | Date: | Thu, 25 Jan 2018 14:52:33 +0000 |
| Subject: | Re: [RFC][DISCUSSION] Collection Inspection | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-101663@lists.php.net to get a copy of this message | ||
On Wed, 24 Jan 2018, Michael Morris wrote:
> Ok, here's another idea I've been mulling over. This I know is possible
> because I've done it using user land code, specifically Drupal 8's
> Assertion\Inspector class.
>
>
> https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Component%21Assertion%21Inspector.php/class/Inspector/8.5.x
>
> These methods provide a means to inspect collections - arrays usually but
> also Traversables. They fill a hole in the PHP library - the ability to
> check collection integrity.
IMO, it makes a lot more sense to check integrity when creating the
"array" structure. Instead, I would suggest to add a native Collection
type, that takes a "type" as argument. They aren't quite full generics,
but it does 1. fix something; 2. isn't really complicated.
What I am suggesting is to add a new syntax "Collection<$type>",
mimicking a class, but having a type as "argument":
$a = new Collection<integer>;
And then $a can act as if you'd use an ArrayAccess'ed class.
Upon each set or update, the type of the value can then be checked
against the type.
Consequently, this would also mean you can type hint on Collection<type>
instead of for example an earlier suggested array of type, where upon
passing in the array each member was checked for its type (slow).
And on top of this, this could be extended to do proper generics too.
cheers,
Derick
--
https://derickrethans.nl | https://xdebug.org | https://dram.io
Like Xdebug? Consider a donation: https://xdebug.org/donate.php,
or become my Patron: https://www.patreon.com/derickr
twitter: @derickr and @xdebug