PHP code example of flashios09 / php-union-types

1. Go to this page and download the library: Download flashios09/php-union-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/ */

    

flashios09 / php-union-types example snippets


UnionTypes::assert(mixed $value, string[] $types, array $options = []): void

  UnionTypes::assert(1.2, ['int', 'float']);
  

  UnionTypes::assert('1.2', ['int', 'float']);
  

  UnionTypes::assert('1.2', ['int', 'float', 'string']);
  

  use \DateTime;

  class Time extends DateTime
  {
    public static function today()
    {
      return new Time();
    }
  }

  $today = Time::today();
  

  // `instanceOf` check is enabled by default
  UnionTypes::assert($today, [\DateTime::class, 'string']);
  

  // disabled using the `instanceOf` option
  UnionTypes::assert($today, [\DateTime::class, 'string', ['instanceOf' => false]]);
  

UnionTypes::is(mixed $value, string[] $types, array $options = []): bool

  UnionTypes::is(1.2, ['int', 'float']);
  

  UnionTypes::is('1.2', ['int', 'float']);
  

  UnionTypes::is('1.2', ['int', 'float', 'string']);
  

  use \DateTime;

  class Time extends DateTime
  {
    public static function today()
    {
      return new Time();
    }
  }

  $today = Time::today();
  

  // `instanceOf` check is enabled by default
  UnionTypes::assert($today, [\DateTime::class, 'string']);
  

  // disabled using the `instanceOf` option
  UnionTypes::assert($today, [\DateTime::class, 'string', ['instanceOf' => false]]);
  

UnionTypes::assertFuncArg(string $argName, string[] $types, array $options = []): void

  function add($a, $b)
  {
      UnionTypes::assertFuncArg('a', ['int', 'float']);
      return $a + $b;
  }
  // invocation
  add(1.2, 1);
  

  class Math
  {
    public static function add($a, $b)
    {
      UnionTypes::assertFuncArg('a', ['int', 'float']);
      return $a + $b;
    }
  }
  // invocation
  Math::add('1.2', 1);
  

  $closure = function ($a, $b) {
      UnionTypes::assertFuncArg('a', ['int', 'float', 'string']);
      return $a + $b;
  };
  // invocation
  $closure('1.2', 1);
  

  use \DateTime;

  class Time extends DateTime
  {
    public static function today()
    {
      return new Time();
    }
  }

  $today = Time::today();
  

  $closure = function ($today) {
      // `instanceOf` check is enabled by default
      UnionTypes::assertFuncArg($today, [\DateTime::class, 'string']);
      // some logic here

      return $today;
  };
  // invocation
  $closure($today);
  

  $closure = function ($today) {
      // disabled using the `instanceOf` option
      UnionTypes::assertFuncArg($today, [\DateTime::class, 'string', ['instanceOf' => false]]);
      // some logic here

      return $today;
  };
  // invocation
  $closure($today);
  

  // config/bootstrap.php
  define('UnionTypes.PATH_TO_APP', '/path/to/app/');

  // using `$_SERVER`(isn't available in **php cli**)
  $PATH_TO_APP = isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR : '';
  define('UnionTypes.PATH_TO_APP', $PATH_TO_APP);
  // using `dirname(__FILE__)`, maybe you need to remove/add some parts, depending on the `__FILE__` location.
  define('UnionTypes.PATH_TO_APP', dirname(__FILE__) . DIRECTORY_SEPARATOR;
  // using the `PWD` key of the `getEnv()` array
  define('UnionTypes.PATH_TO_APP', getEnv()['PWD'] . DIRECTORY_SEPARATOR;