1. Go to this page and download the library: Download sj-i/typist 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/ */
sj-i / typist example snippets
use Typist\Typist;
// type enforcements are valid during the lifetime of this `$_`
$_ = [
Typist::int($typed_int, 1),
Typist::string($typed_string, 'str'),
Typist::bool($typed_bool, false),
Typist::float($typed_float, 0.1),
Typist::class(\DateTimeInterface::class, $typed_object, new \DateTime()),
];
assert($typed_int === 1);
assert($typed_string === 'str');
assert($typed_bool === false);
assert($typed_float === 0.1);
assert($typed_object instanceof \DateTime);
// modifications with valid types are OK
$typed_int = 2;
$typed_string = 'trs';
$typed_bool = true;
$typed_float = -0.1;
$typed_object = new DateTimeImmutable();
// any statements below raises TypeError
$typed_int = 'a';
$typed_string = 1;
$typed_bool = 'a';
$typed_float = 'a';
$typed_object = 'a';
use function Typist\int;
use function Typist\float;
use function Typist\string;
use function Typist\bool;
use function Typist\class_; // trailing underscore is needed
$_ = [
int($typed_int, 1),
string($typed_string, 'str'),
bool($typed_bool, false),
float($typed_float, 0.1),
class_(\DateTimeInterface::class, $typed_object, new \DateTime()),
];
use function Typist\nullable_int;
use function Typist\nullable_float;
use function Typist\nullable_string;
use function Typist\nullable_bool;
use function Typist\nullable_class; // trailing underscore is not needed
$_ = [
nullable_int($typed_int, null),
nullable_string($typed_string, null),
nullable_bool($typed_bool, null),
nullable_float($typed_float, null),
nullable_class(\DateTimeInterface::class, $typed_object, null),
];
$a = 1;
$b =& $a;
$c =& $b;
$a = 1;
$b =& $a;
$c =& $b;
$o = new class() {
public int $a = 1;
public float $b;
};
$o->b =& $o->a;
$c =& $o->b;
$o->a = 1; // legal
$o->a = 'abc'; // TypeError will be thrown. $o->a is declared as int
$o->b = 1.5; // $o->b is declared as float, but...
var_dump($o->a); // $o->a is declared as int, so this must not be `1.5`!
$c = new DateTime(); // $c is a local variable, so doesn't have any type constraint itself, but...
var_dump($o->a, $o->b); // Neither $o->a and $o->b must not be a DateTime!!!
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.