PHP code example of std-library / type-guard

1. Go to this page and download the library: Download std-library/type-guard 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/ */

    

std-library / type-guard example snippets


function config(): mixed;

// At compile time, the type of $apiKey is `mixed`:
$apiKey = config('api_key');

// We instruct the static analyzer that $apiKey is a `string`:
$apiKey = type($apiKey)->asString();

/** @var array<int, User>|null $users */
$users = getUsers();

// Narrows down the type to `array<int, User>`
$users = type($users)->not()->null();

/** @var Collection<int, User>|null $users */
$users = getUsers();

// Narrows down the type to `Collection<int, User>`
$users = type($users)->as(Collection::class);

$variable = type($variable)->as(User::class);

$variable = type($variable)->asInt();

$variable = type($variable)->asFloat();

$variable = type($variable)->asString();

$variable = type($variable)->asBool();

$variable = type($variable)->asNull();

$variable = type($variable)->asCallable();

$variable = type($variable)->not()->null();

$variable = type($variable)->asArray();

$variable = type($variable)->asIterable();