{"id":18818717,"url":"https://github.com/chrisharrison/php-array-of","last_synced_at":"2026-03-10T08:03:22.980Z","repository":{"id":54304354,"uuid":"106537483","full_name":"chrisharrison/php-array-of","owner":"chrisharrison","description":"Implement an array of a defined type. Generics replacement for PHP.","archived":false,"fork":false,"pushed_at":"2021-02-25T10:54:11.000Z","size":21,"stargazers_count":10,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-27T14:47:55.358Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chrisharrison.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-10-11T10:03:30.000Z","updated_at":"2024-09-19T04:46:17.000Z","dependencies_parsed_at":"2022-08-13T11:30:51.463Z","dependency_job_id":null,"html_url":"https://github.com/chrisharrison/php-array-of","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/chrisharrison/php-array-of","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisharrison%2Fphp-array-of","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisharrison%2Fphp-array-of/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisharrison%2Fphp-array-of/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisharrison%2Fphp-array-of/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chrisharrison","download_url":"https://codeload.github.com/chrisharrison/php-array-of/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisharrison%2Fphp-array-of/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30327560,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-10T05:25:20.737Z","status":"ssl_error","status_checked_at":"2026-03-10T05:25:17.430Z","response_time":106,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T00:18:01.487Z","updated_at":"2026-03-10T08:03:22.948Z","avatar_url":"https://github.com/chrisharrison.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# php-array-of\n\n[![Build Status](https://travis-ci.org/chrisharrison/php-array-of.svg?branch=master)](https://travis-ci.org/chrisharrison/php-array-of)\n\nImplement an array of a defined type. Generics replacement for PHP.\n\n## Requirements ##\n\nRequires PHP \u003e= 7.1\n\n## Installation ##\n\nThrough Composer, obviously:\n\n```\ncomposer require chrisharrison/php-array-of\n```\n\n## Why? ##\n\nPHP 7 has pretty good support for type declarations, both in arguments and returns. It now also handles primitives as well as class names. For example:\n\n```php\npublic function dealCard(string $cardName): Card;\n```\n\nOne thing is still lacking. Array generics. There's an [RFC](https://wiki.php.net/rfc/arrayof) for implementing 'Array Of'. It's been around for quite a while and was roundly rejected.\n\nThis library is a workaround for that. It allows you to make type declarations for arrays of a particular type.\n\n## Usage ##\n\n### Using an existing implementation ###\n\nThe library comes with `ArrayOf` implementations for all of the PHP scalar types. i.e.:\n\n* `ArrayOfInteger`\n* `ArrayOfFloat`\n* `ArrayOfString`\n* `ArrayOfBoolean`\n\nThese can then be used in a type declaration:\n\n```php\npublic function getIntegers(): ArrayOfInteger;\n```\n\nAn `ArrayOfInteger` can be created:\n\n```php\n$integers = new ArrayOfInteger([1,1,2,3,5,8,13]);\n```\n\nand used like an array:\n\n```php\n$sum = $integers[5] + $integers[6]; // equals 21\n```\n\n### Implementing your own type ###\n\nYou can create your own `ArrayOf`s for your own types.\n\n```php\nfinal class ArrayOfCard extends ArrayOf\n{\n    protected function typeToEnforce(): string\n    {\n        return Card::class;\n    }\n}\n```\n\nAn `ArrayOfCard` can be created thus:\n\n```php\n$aceOfSpades = new Card('spades', 'ace');\n$threeOfClubs = new Card('clubs', '3');\n\n$cards = new ArrayOfCard([$aceOfSpades, $threeOfClubs]);\n```\n\n## Other concerns ##\n\n### Enforcement ###\n\nMembers of an `ArrayOf` are enforced as being of the type specified in the `typeToEnforce` abstract method. This enforcement occurs on instantiation at runtime. If you try to instantiate with a member of a non-matching type, an exception will be thrown.\n\n### Permissible types ###\n\nOnly PHP [scalars](http://php.net/manual/en/function.is-scalar.php) and objects can be members of an `ArrayOf`. So no `callable`s and no `array`s. \n\n### Immutability ###\n\n`ArrayOf`s are mutable. This library provides the capability to define immutable objects in the form of `ImmutableArrayOf`s. These objects extend from `ArrayOf` and work in the same way except after the initial instantiation, no further changes can be made to the object. If you try to perform a write operation (e.g. `unset`) on it, an exception will be thrown. All of the scalar types are also provided in immutable form:\n\n* `ImmutableArrayOfInteger`\n* `ImmutableArrayOfFloat`\n* `ImmutableArrayOfString`\n* `ImmutableArrayOfBoolean`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrisharrison%2Fphp-array-of","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchrisharrison%2Fphp-array-of","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrisharrison%2Fphp-array-of/lists"}