PHP code example of joshbrw / type-enforcement

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

    

joshbrw / type-enforcement example snippets


class UserRegistrar {
    public function register(array $usersDetails, $author)
    {
        if (!$author instanceof SomeAuthor) {
            throw new \InvalidArgumentException('The author must be an instance of SomeAuthor');
        }
    }
}

use Joshbrw\TypeEnforcement\Type;

class UserRegistrar {
    public function register(array $usersDetails, $author)
    {
        /* Throws \InvalidArgumentException on invalid input */
        Type::enforce($author, SomeAuthor::class);
    }
}

Type::enforce($variable, Type::class, 'The variable must be a Type!');

Expected [Tests\NonExistentClass], [array '["array"]'] provided.