Download the PHP package aziule/typed-collections without Composer
On this page you can find all versions of the php package aziule/typed-collections. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download aziule/typed-collections
More information about aziule/typed-collections
Files in aziule/typed-collections
Package typed-collections
Short Description Adding some typing in arrays or object collections
License MIT
Informations about the package typed-collections
Typed Collections
TL;DR
typed-collections is a project aiming to sensibilise developers to write good code and documentation when it comes to manipulating arrays in PHP.
Jump to the documentation
PHP's dynamically typed nature can lead bad developers to write abominable code. This is especially true for collections (arrays) of items, where one can only rely on the PHPDoc (if any, up-to-date and accurate) or on a variable's name to guess what the array actually contains:
Absolute worst case:
Bad - and unfortunately, too common:
Common practice
Installation
Install it via Composer:
Documentation
Supported types
The library supports most of PHP's primitive types, as well as user-defined objects collections:
- Array
- Boolean
- Double
- Int
- String
- User-defined Object
Usage
For primitive types
Primitive type collections will allow to store only items of this type (only ints, only booleans, etc.).
Here is the list of available primitive collection classes:
Empty collection
Pre-filled collection
For user-defined objects
Two different methods exist for creating and passing user-defined objects collections:
- Using
ObjectCollection
- Creating a custom collection extending
ObjectCollection
Using ObjectCollection
Example
Just pass an array of objects as a second argument of the ObjectCollection
constructor to initialise it:
If you need to know what kind of object are embedded in the ObjectCollection
:
Creating a custom collection
If you want to pass an even more strongly-typed collection, you can create a custom collection for each type of object you need.
Example: