PHP code example of edgaralexanderfr / php-types

1. Go to this page and download the library: Download edgaralexanderfr/php-types library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

edgaralexanderfr / php-types example snippets




declare(strict_types=1);

\string_array;

$message = string_array('Hello', 'world', '!');
print_r($message);



declare(strict_types=1);

\byte;
use function PHPTypes\Primitive\char;
use function PHPTypes\Primitive\uchar;

/** @var byte (Extends from `uint8_t`). */
$byte = byte(255);

/** @var char */
$char = char('c');

/** @var uchar */
$uchar = uchar('A');

/** @var char */
$string = char('Hello!'); // Since `char` can only store a single char, `$string` will be '!' and a PHP Warning will be notified.

echo $byte . PHP_EOL;
echo $char . PHP_EOL;
echo $uchar . PHP_EOL;
echo $string . PHP_EOL;



declare(strict_types=1);

;

use function PHPTypes\Primitive\object_t;

function display(object_t $object): void
{
    echo "{$object->name}:" . PHP_EOL;
    echo $object->json() . PHP_EOL;
}

$object = object_t([
    "name" => "Ford Mustang GT",
    "brand" => "Ford",
    "category" => "Muscle Car",
    "gas" => 0.8,
    "engine" => object_t([
        "type" => "V8",
        "rpm" => 750,
    ]),
    "transmission" => object_t([
        "type" => "manual",
        "gear" => 1,
        "gears" => ["R", "N", "1", "2", "3", "4", "5", "6"],
    ]),
]);

display($object);



declare(strict_types=1);

ay;

use function PHPTypes\Primitive\bool_array;

function display(bool_array $array): void
{
    foreach ($array as $value) {
        echo $value . PHP_EOL;
    }
}

$array = bool_array(false, true, false);
display($array);



declare(strict_types=1);

y;

use function PHPTypes\Primitive\int_array;

function display(int_array $array): void
{
    foreach ($array as $value) {
        echo $value . PHP_EOL;
    }
}

$array = int_array(1, 2, 3);
display($array);



declare(strict_types=1);

ray;

use function PHPTypes\Primitive\float_array;

function display(float_array $array): void
{
    foreach ($array as $value) {
        echo $value . PHP_EOL;
    }
}

$array = float_array(0.1, 2.3, 4.5);
display($array);



declare(strict_types=1);

rray;

use function PHPTypes\Primitive\string_array;

function display(string_array $array): void
{
    foreach ($array as $value) {
        echo $value . PHP_EOL;
    }
}

$array = string_array('🍎', '🍊', '🍌');
display($array);



declare(strict_types=1);

rray_t;

use function PHPTypes\Primitive\object_t;
use function PHPTypes\Primitive\object_array_t;

function display(object_array_t $array): void
{
    foreach ($array as $value) {
        print_r($value);
    }
}

$array = object_array_t(
    object_t([
        'id' => 1,
        'name' => 'Charles Babbage',
    ]),
    object_t([
        'id' => 2,
        'name' => 'Alan Turing',
    ]),
    object_t([
        'id' => 3,
        'name' => 'Edsger Dijkstra',
    ]),
);

display($array);



declare(strict_types=1);

\byte_array;
use function PHPTypes\Primitive\char_array;
use function PHPTypes\Primitive\uchar_array;

$byte_array = byte_array(0, 255, 0, 255);

$char_array = char_array('H', 'e', 'l', 'l', 'o', '!');

$uchar_array = uchar_array('a', 'b', 'c');

var_dump($byte_array);

// Notice how we can print out `char_array`s just like usual strings:
echo $char_array . PHP_EOL;
echo $uchar_array . PHP_EOL;



declare(strict_types=1);

_t;
use function PHPTypes\Std\int8_t;
use function PHPTypes\Std\uint16_t;
use function PHPTypes\Std\uint8_t;

/** @var int8_t [-128...127] */
$int8_t = int8_t(0);

/** @var uint8_t [0...255] */
$uint8_t = uint8_t(0);

/** @var int16_t [-32768...32767] */
$int16_t = int16_t(0);

/** @var uint16_t [0...65535] */
$uint16_t = uint16_t(0);

// Assigning/accessing values:
$int8_t->value = 256;
$uint8_t->value = -128;
$int16_t->value = 1024;
$uint16_t->value = 2048;

// Displaying new values
// (Notice how the first 2 variables overflow):
echo $int8_t . PHP_EOL;
echo $uint8_t . PHP_EOL;
echo $int16_t . PHP_EOL;
echo $uint16_t . PHP_EOL;



declare(strict_types=1);

t;

/** @var size_t [0...PHP_INT_MAX] */
$size_t = size_t(0);
$size_t->value--;
echo $size_t->value . PHP_EOL;



declare(strict_types=1);

e PHPTypes\Std\Int8Array;
use PHPTypes\Std\UInt16Array;
use PHPTypes\Std\UInt8Array;

use function PHPTypes\Std\int16_array;
use function PHPTypes\Std\int8_array;
use function PHPTypes\Std\size_array;
use function PHPTypes\Std\uint16_array;
use function PHPTypes\Std\uint8_array;

/** @var int8_array */
$int8_array = int8_array(-3, -2, -1);

/** @var uint8_array */
$uint8_array = uint8_array(0, 1, 2);

/** @var int16_array */
$int16_array = int16_array(3, 4, 5);

/** @var uint16_array */
$uint16_array = uint16_array(6, 7, 8);

/** @var size_array */
$size_array = size_array(9, 10, 11);

// or:

/** @var Int8Array */
$int8_array = new Int8Array(-3, -2, -1);

/** @var UInt8Array */
$uint8_array = new UInt8Array(0, 1, 2);

/** @var Int16Array */
$int16_array = new Int16Array(3, 4, 5);

/** @var UInt16Array */
$uint16_array = new UInt16Array(6, 7, 8);



declare(strict_types=1);

 = 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;
}



declare(strict_types=1);

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;
}



declare(strict_types=1);



$set = new StringHashSet();
$set->add('🍎');
$set->add('🍊');
$set->add('🍌');
$set->add('🍌');
$set->add('🥭');

foreach ($set as $value) {
    echo $value . PHP_EOL;
}



declare(strict_types=1);

\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);



declare(strict_types=1);

\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;
}



declare(strict_types=1);

\string_array;

$fruits = string_array('🍎', '🍊', '🥭', '🍌');
[$without_mango, $mango] = $fruits->splice(2, 1);

print_r($without_mango);
print_r($mango);



declare(strict_types=1);

bytes = new UInt8Array(0, 1, 2, 4, 8, 16, 32, 64, 128);

echo $bytes->length . PHP_EOL;



declare(strict_types=1);

s User
{
    public function __construct(
        public readonly int $id,
        public readonly string $name,
    ) {}
}

class UserArray extends ArrayObject
{
    public function __construct(User ...$values)
    {
        parent::__construct($values);
    }
}

$users = new UserArray(
    new User(1, 'John'),
    new User(2, 'Doe'),
);

foreach ($users as $user) {
    echo "{$user->id} => {$user->name}" . PHP_EOL;
}



declare(strict_types=1);

tice the usage of `ArrayObject`

use function PHPTypes\typedef;

class User
{
    public function __construct(
        public readonly int $id,
        public readonly string $name,
    ) {}
}

typedef('array', 'User', 'UserArray');

/**
 * @disregard
 * @var ArrayObject|User[]
 */
$users = new UserArray(
    new User(1, 'John'),
    new User(2, 'Doe'),
);

foreach ($users as $user) {
    echo "{$user->id} => {$user->name}" . PHP_EOL;
}



declare(strict_types=1);

namespace App;

f `ArrayObject`

use function PHPTypes\typedef;

class User
{
    public function __construct(
        public readonly int $id,
        public readonly string $name,
    ) {}
}

typedef('array', 'App\User', 'UserArray');

/**
 * @disregard
 * @var ArrayObject|User[]
 */
$users = new \App\UserArray(
    new \App\User(1, 'John'),
    new \App\User(2, 'Doe'),
);

foreach ($users as $user) {
    echo "{$user->id} => {$user->name}" . PHP_EOL;
}
bash
composer 
bash
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
bash
mkdir lib \
&& curl -L -o lib/php-types.phar https://github.com/edgaralexanderfr/php-types/raw/master/lib/php-types.phar
bash
php examples/bool_array.php

1

bash
php examples/int_array.php
1
2
3
bash
php examples/float_array.php
0.1
2.3
4.5
bash
php examples/string_array.php
🍎
🍊
🍌
bash
php examples/stdint.php
0
128
1024
2048
bash
php examples/size_t.php
9223372036854775806
bash
php examples/hash_set.php
1
two
3
one
bash
php examples/int_hash_set.php
1
2
3
0
bash
php examples/string_hash_set.php
🍎
🍊
🍌
🥭
bash
php examples/has_any_is_empty.php
`$fruits` contains at least 1 element.
`$objects` is an empty array.
bash
php examples/splice.php
PHPTypes\Primitive\string_array Object
(
    [type:protected] => string
    [storage:ArrayIterator:private] => Array
        (
            [0] => 🍎
            [1] => 🍊
            [2] => 🍌
        )

)
PHPTypes\Primitive\string_array Object
(
    [type:protected] => string
    [storage:ArrayIterator:private] => Array
        (
            [0] => 🥭
        )

)
bash
php examples/length.php
9
bash
php examples/typedef_1.php
1 => John
2 => Doe
bash
php examples/typedef_2.php
1 => John
2 => Doe
bash
php examples/typedef_3.php
1 => John
2 => Doe