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/ */
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);
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);
\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);
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;
}