PHP Types is a small library/framework aimed to improve and encourage strong typing across your project's codebase, consisting of basic array-types, useful data structures and more, that are not natively available by the language itself.
- PHP 8.3.0 or major
- Composer (optional)
- Have an initted Composer project (optional)
Install PHP Types via Composer:
composer require edgaralexanderfr/php-types
or:
You can always download the library as .zip file, decompress it, store it somewhere in your target project and include the autoload.php file from the library's project root:
curl -L -o php-types-master.zip https://github.com/edgaralexanderfr/php-types/archive/refs/heads/master.zip \
&& unzip php-types-master.zip \
&& rm php-types-master.zip
or:
You can download the packed .phar version of the library:
mkdir lib \
&& curl -L -o lib/php-types.phar https://github.com/edgaralexanderfr/php-types/raw/master/lib/php-types.phar
<?php
declare(strict_types=1);
include 'lib/php-types.phar';
use function PHPTypes\Primitive\string_array;
$message = string_array('Hello', 'world', '!');
print_r($message);
<?php
declare(strict_types=1);
include 'vendor/autoload.php';
use PHPTypes\Primitive\object_t;
use function PHPTypes\Primitive\object;
function display(object_t $object): void
{
echo "{$object->name}:" . PHP_EOL;
echo $object->json() . PHP_EOL;
}
$object = object([
'name' => 'Ford Mustang GT',
'brand' => 'Ford',
'category' => 'Muscle Car',
'gas' => 0.8,
'engine' => object([
'type' => 'V8',
'rpm' => 750,
]),
'transmission' => object([
'type' => 'manual',
'status' => 1,
'gears' => ['R', 'N', '1', '2', '3', '4', '5', '6'],
]),
]);
display($object);
php examples/object.php
Ford Mustang GT:
{"name":"Ford Mustang GT","brand":"Ford","category":"Muscle Car","gas":0.8,"engine":{"type":"V8","rpm":750},"transmission":{"type":"manual","status":1,"gears":["R","N","1","2","3","4","5","6"]}}
<?php
declare(strict_types=1);
include 'vendor/autoload.php';
use PHPTypes\Primitive\bool_array_t;
use function PHPTypes\Primitive\bool_array;
function display(bool_array_t $array): void
{
foreach ($array as $value) {
echo $value . PHP_EOL;
}
}
$array = bool_array(false, true, false);
display($array);
php examples/bool_array.php
1
<?php
declare(strict_types=1);
include 'vendor/autoload.php';
use PHPTypes\Primitive\int_array_t;
use function PHPTypes\Primitive\int_array;
function display(int_array_t $array): void
{
foreach ($array as $value) {
echo $value . PHP_EOL;
}
}
$array = int_array(1, 2, 3);
display($array);
php examples/int_array.php
1
2
3
<?php
declare(strict_types=1);
include 'vendor/autoload.php';
use PHPTypes\Primitive\float_array_t;
use function PHPTypes\Primitive\float_array;
function display(float_array_t $array): void
{
foreach ($array as $value) {
echo $value . PHP_EOL;
}
}
$array = float_array(0.1, 2.3, 4.5);
display($array);
php examples/float_array.php
0.1
2.3
4.5
<?php
declare(strict_types=1);
include 'vendor/autoload.php';
use PHPTypes\Primitive\string_array_t;
use function PHPTypes\Primitive\string_array;
function display(string_array_t $array): void
{
foreach ($array as $value) {
echo $value . PHP_EOL;
}
}
$array = string_array('π', 'π', 'π');
display($array);
php examples/string_array.php
π
π
π
<?php
declare(strict_types=1);
include 'vendor/autoload.php';
use PHPTypes\Primitive\object_array_t;
use function PHPTypes\Primitive\object;
use function PHPTypes\Primitive\object_array;
function display(object_array_t $array): void
{
foreach ($array as $value) {
print_r($value);
}
}
$array = object_array(
object([
'id' => 1,
'name' => 'Charles Babbage',
]),
object([
'id' => 2,
'name' => 'Alan Turing',
]),
object([
'id' => 3,
'name' => 'Edsger Dijkstra',
]),
);
display($array);
php examples/object_array.php
PHPTypes\Primitive\object_t Object
(
[id] => 1
[name] => Charles Babbage
)
PHPTypes\Primitive\object_t Object
(
[id] => 2
[name] => Alan Turing
)
PHPTypes\Primitive\object_t Object
(
[id] => 3
[name] => Edsger Dijkstra
)
<?php
declare(strict_types=1);
include 'vendor/autoload.php';
use PHPTypes\Set\HashSet;
$set = new HashSet();
$set->add(1);
$set->add('two');
$set->add(3);
$set->add(3);
$set->add('two');
$set->add('one');
foreach ($set as $value) {
echo $value . PHP_EOL;
}
php examples/hash_set.php
1
two
3
one
<?php
declare(strict_types=1);
include 'vendor/autoload.php';
use PHPTypes\Set\IntHashSet;
$set = new IntHashSet();
$set->add(1);
$set->add(2);
$set->add(3);
$set->add(3);
$set->add(2);
$set->add(1);
$set->add(0);
foreach ($set as $value) {
echo $value . PHP_EOL;
}
php examples/int_hash_set.php
1
2
3
0
<?php
declare(strict_types=1);
include 'vendor/autoload.php';
use PHPTypes\Set\StringHashSet;
$set = new StringHashSet();
$set->add('π');
$set->add('π');
$set->add('π');
$set->add('π');
$set->add('π₯');
foreach ($set as $value) {
echo $value . PHP_EOL;
}
php examples/string_hash_set.php
π
π
π
π₯
In case you attempt to assign a value with a type different than an array's type, a TypeError
is thrown:
<?php
declare(strict_types=1);
include 'vendor/autoload.php';
use function PHPTypes\Primitive\int_array;
$array = int_array(1, 2, 3);
try {
$array[2] = 'π₯';
} catch (TypeError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
$array[] = 'π₯';
} catch (TypeError $e) {
echo $e->getMessage() . PHP_EOL;
}
$array[] = 4;
print_r($array);
php examples/type_error.php
Element must be of type integer, string given, called
Element must be of type integer, string given, called
PHPTypes\Primitive\int_array_t Object
(
[type:protected] => integer
[storage:ArrayIterator:private] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
)
Arrays contain useful methods that help to determine whether if they're empty or contain at least 1 element in them:
<?php
declare(strict_types=1);
include 'vendor/autoload.php';
use function PHPTypes\Primitive\object_array;
use function PHPTypes\Primitive\string_array;
$fruits = string_array('π₯');
if ($fruits->hasAny()) {
echo '`$fruits` contains at least 1 element.' . PHP_EOL;
}
$objects = object_array();
if ($objects->isEmpty()) {
echo '`$objects` is an empty array.' . PHP_EOL;
}
php examples/has_any_is_empty.php
`$fruits` contains at least 1 element.
`$objects` is an empty array.