Download the PHP package chrisharrison/php-array-of without Composer
On this page you can find all versions of the php package chrisharrison/php-array-of. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download chrisharrison/php-array-of
More information about chrisharrison/php-array-of
Files in chrisharrison/php-array-of
Package php-array-of
Short Description Implement an array of a defined type. Generics replacement for PHP.
License MIT
Informations about the package php-array-of
php-array-of
Implement an array of a defined type. Generics replacement for PHP.
Requirements
Requires PHP >= 7.1
Installation
Through Composer, obviously:
Why?
PHP 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:
One thing is still lacking. Array generics. There's an RFC for implementing 'Array Of'. It's been around for quite a while and was roundly rejected.
This library is a workaround for that. It allows you to make type declarations for arrays of a particular type.
Usage
Using an existing implementation
The library comes with ArrayOf
implementations for all of the PHP scalar types. i.e.:
ArrayOfInteger
ArrayOfFloat
ArrayOfString
ArrayOfBoolean
These can then be used in a type declaration:
An ArrayOfInteger
can be created:
and used like an array:
Implementing your own type
You can create your own ArrayOf
s for your own types.
An ArrayOfCard
can be created thus:
Other concerns
Enforcement
Members 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.
Permissible types
Only PHP scalars and objects can be members of an ArrayOf
. So no callable
s and no array
s.
Immutability
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:
ImmutableArrayOfInteger
ImmutableArrayOfFloat
ImmutableArrayOfString
ImmutableArrayOfBoolean