<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/i-went-deep-and-wrote-a-fairly-detailed-proposal-for-typed-array-in-php-php-function-processuse-rxnr7fvvb" -->

---
title: I went deep and wrote a fairly detailed proposal for...
description: PHP lacks support for generics, posing challenges for typed arrays which are essential for frameworks and tools relying on precise type definitions. The...
canonical: https://daily.dev/posts/i-went-deep-and-wrote-a-fairly-detailed-proposal-for-typed-array-in-php-php-function-processuse-rxnr7fvvb
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: I went deep and wrote a fairly detailed proposal for typed array in PHP.  ```php function processUserConsent(bool $accepted, array<string> $categories) {     if ($accepted) { 	    setcookie('consent', implode(",", $categories), time() + 24*60*60*365); 	} else { 	    setcookie('consent', '', 1); 	} }  $post = [     "accepted" => true, 	"categories" => ["necessary", "preferences"] ];  processUserConsent(...$post); ```  This is a very basic example, but the proposal considers many implementation details, backwards compatibility, future optimization, and features like reflection and references.  This looks like generics, and in a sense, it is - but for arrays only. It is designed to be compatible with a possible future generics feature (another proposal I worked on in depth previously) however it attempts to start with a much smaller feature, since the implementation details likely wouldn't overlap considerably with full-blown generics.  Note that this is a very different proposal from the [rejected 2014 proposal](https://wiki.php.net/rfc/arrayof) (both in terms of syntax and scope) and attempts to address most of the critique I could find in the mailing list activity from back then.  I hope this is enough to restart the conversation and motivate the community to reconsider this feature. 🙂✌️ | daily.dev
og:description: PHP lacks support for generics, posing challenges for typed arrays which are essential for frameworks and tools relying on precise type definitions. The...
og:url: https://daily.dev/posts/i-went-deep-and-wrote-a-fairly-detailed-proposal-for-typed-array-in-php-php-function-processuse-rxnr7fvvb
og:image: https://api.daily.dev/og/posts/Rxnr7Fvvb.png
og:image:alt: I went deep and wrote a fairly detailed proposal for typed array in PHP.  ```php function processUserConsent(bool $accepted, array<string> $categories) {     if ($accepted) { 	    setcookie('consent', implode(",", $categories), time() + 24*60*60*365); 	} else { 	    setcookie('consent', '', 1); 	} }  $post = [     "accepted" => true, 	"categories" => ["necessary", "preferences"] ];  processUserConsent(...$post); ```  This is a very basic example, but the proposal considers many implementation details, backwards compatibility, future optimization, and features like reflection and references.  This looks like generics, and in a sense, it is - but for arrays only. It is designed to be compatible with a possible future generics feature (another proposal I worked on in depth previously) however it attempts to start with a much smaller feature, since the implementation details likely wouldn't overlap considerably with full-blown generics.  Note that this is a very different proposal from the [rejected 2014 proposal](https://wiki.php.net/rfc/arrayof) (both in terms of syntax and scope) and attempts to address most of the critique I could find in the mailing list activity from back then.  I hope this is enough to restart the conversation and motivate the community to reconsider this feature. 🙂✌️
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# I went deep and wrote a fairly detailed proposal for typed array in PHP.

```php
function processUserConsent(bool $accepted, array<string> $categories) {
    if ($accepted) {
	    setcookie('consent', implode(",", $categories), time() + 24*60*60*365);
	} else {
	    setcookie('consent', '', 1);
	}
}

$post = [
    "accepted" => true,
	"categories" => ["necessary", "preferences"]
];

processUserConsent(...$post);
```

This is a very basic example, but the proposal considers many implementation details, backwards compatibility, future optimization, and features like reflection and references.

This looks like generics, and in a sense, it is - but for arrays only. It is designed to be compatible with a possible future generics feature (another proposal I worked on in depth previously) however it attempts to start with a much smaller feature, since the implementation details likely wouldn't overlap considerably with full-blown generics.

Note that this is a very different proposal from the [rejected 2014 proposal](https://wiki.php.net/rfc/arrayof) (both in terms of syntax and scope) and attempts to address most of the critique I could find in the mailing list activity from back then.

I hope this is enough to restart the conversation and motivate the community to reconsider this feature. 🙂✌️

**[PHP Dev](https://daily.dev/sources/phpdev)** · [@mindplay](https://daily.dev/mindplay) · 8 upvotes · 3 comments

## Summary

PHP lacks support for generics, posing challenges for typed arrays which are essential for frameworks and tools relying on precise type definitions. The proposal aims to introduce a native solution for typed arrays using syntax like `array<Type>` to enhance both runtime type-checking and IDE support for array types. This proposal ensures compatibility with potential future generic additions and provides various examples to demonstrate the behaviors and principles of typed arrays, such as value-passing, type-casting, nesting, and interaction with existing PHP type systems. Performance impacts and future optimizations are also considered.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://wiki.php.net/rfc/generic-arrays>

## Community discussion

Top comments from developers on daily.dev.

**@hafijul233** · 0 upvotes

> It was awesome

**@a\_h\_abid** · 0 upvotes

> Hope I can get to use it soon...

---

Tags: [#webdev](https://daily.dev/tags/webdev), [#php](https://daily.dev/tags/php)

[View this post on daily.dev](https://daily.dev/posts/i-went-deep-and-wrote-a-fairly-detailed-proposal-for-typed-array-in-php-php-function-processuse-rxnr7fvvb)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"DiscussionForumPosting","mainEntityOfPage":"https://daily.dev/posts/i-went-deep-and-wrote-a-fairly-detailed-proposal-for-typed-array-in-php-php-function-processuse-rxnr7fvvb","headline":"I went deep and wrote a fairly detailed proposal for typed array in PHP.\n\n```php\nfunction processUserConsent(bool $accepted, array\u003cstring\u003e $categories) {\n    if ($accepted) {\n\t    setcookie('consent', implode(\",\", $categories), time() + 24*60*60*365);\n\t} else {\n\t    setcookie('consent', '', 1);\n\t}\n}\n\n$post = [\n    \"accepted\" =\u003e true,\n\t\"categories\" =\u003e [\"necessary\", \"preferences\"]\n];\n\nprocessUserConsent(...$post);\n```\n\nThis is a very basic example, but the proposal considers many implementation details, backwards compatibility, future optimization, and features like reflection and references.\n\nThis looks like generics, and in a sense, it is - but for arrays only. It is designed to be compatible with a possible future generics feature (another proposal I worked on in depth previously) however it attempts to start with a much smaller feature, since the implementation details likely wouldn't overlap considerably with full-blown generics.\n\nNote that this is a very different proposal from the [rejected 2014 proposal](https://wiki.php.net/rfc/arrayof) (both in terms of syntax and scope) and attempts to address most of the critique I could find in the mailing list activity from back then.\n\nI hope this is enough to restart the conversation and motivate the community to reconsider this feature. 🙂✌️","text":"Shared: PHP: rfc:generic-arrays","url":"https://daily.dev/posts/i-went-deep-and-wrote-a-fairly-detailed-proposal-for-typed-array-in-php-php-function-processuse-rxnr7fvvb","datePublished":"2024-12-13T21:44:13.015Z","dateModified":"2024-12-13T21:44:45.566Z","author":{"@type":"Person","name":"Rasmus Schultz","url":"https://daily.dev/mindplay","image":"https://lh3.googleusercontent.com/a/ACg8ocKveyfHGXtOtaXw2W7ZVygFdp8TEe1mol5xCZhsqm0V4828vKnhgg=s96-c","description":"Full stack web development since 1998. Learning UI/UX design.","interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"EndorseAction"},"userInteractionCount":420}},"image":"https://media.daily.dev/image/upload/s--VDukGCjf--/f_auto/v1722860399/public/Placeholder%2002","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":8},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":3}],"sharedContent":{"@type":"WebPage","url":"https://api.daily.dev/r/sXgQhC7VZ"},"comment":[{"@type":"Comment","text":"It was awesome","datePublished":"2025-01-08T10:51:06.083Z","url":"https://daily.dev/posts/Rxnr7Fvvb#c-sdsVvFVh1","author":{"@type":"Person","name":"Hafijul Islam","url":"https://daily.dev/hafijul233","image":"https://avatars.githubusercontent.com/u/24684511?v=4"}},{"@type":"Comment","text":"Hope I can get to use it soon…","datePublished":"2024-12-14T01:44:43.267Z","url":"https://daily.dev/posts/Rxnr7Fvvb#c-1rqpC0eJ0","author":{"@type":"Person","name":"Ahmedul Haque Abid","url":"https://daily.dev/a_h_abid","image":"https://avatars2.githubusercontent.com/u/2020773?v=4"}}],"isPartOf":{"@type":"WebPage","url":"https://daily.dev/squads/phpdev","name":"PHP Dev"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"PHP Dev","item":"https://daily.dev/squads/phpdev"},{"@type":"ListItem","position":3,"name":"I went deep and wrote a fairly detailed proposal for typed array in PHP.\n\n```php\nfunction processUserConsent(bool $accepted, array\u003cstring\u003e $categories) {\n    if ($accepted) {\n\t    setcookie('consent', implode(\",\", $categories), time() + 24*60*60*365);\n\t} else {\n\t    setcookie('consent', '', 1);\n\t}\n}\n\n$post = [\n    \"accepted\" =\u003e true,\n\t\"categories\" =\u003e [\"necessary\", \"preferences\"]\n];\n\nprocessUserConsent(...$post);\n```\n\nThis is a very basic example, but the proposal considers many implementation details, backwards compatibility, future optimization, and features like reflection and references.\n\nThis looks like generics, and in a sense, it is - but for arrays only. It is designed to be compatible with a possible future generics feature (another proposal I worked on in depth previously) however it attempts to start with a much smaller feature, since the implementation details likely wouldn't overlap considerably with full-blown generics.\n\nNote that this is a very different proposal from the [rejected 2014 proposal](https://wiki.php.net/rfc/arrayof) (both in terms of syntax and scope) and attempts to address most of the critique I could find in the mailing list activity from back then.\n\nI hope this is enough to restart the conversation and motivate the community to reconsider this feature. 🙂✌️"}]}
```

