PHP code example of jojo1981 / php-types

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

    

jojo1981 / php-types example snippets




gerType = \Jojo1981\PhpTypes\AbstractType::createFromTypeName('int');
$integerType->isAssignableValue('text'); // will return false
$integerType->isAssignableValue(28); // will return true

$floatType1 = \Jojo1981\PhpTypes\AbstractType::createFromValue(5.0);
$floatType1->isAssignableValue(true); // will return false
$floatType1->isAssignableValue('text'); // will return false
$floatType1->isAssignableValue(10); // will return false
$floatType1->isAssignableValue(10.0); // will return true

$floatType2 = \Jojo1981\PhpTypes\AbstractType::createFromTypeName('number');
$floatType2->isAssignableValue(true); // will return false
$floatType2->isAssignableValue('text'); // will return false
$floatType2->isAssignableValue(1); // will return false
$floatType2->isAssignableValue(1.0); // will return true

$stringType = \Jojo1981\PhpTypes\AbstractType::createFromValue('string');
$stringType->isAssignableValue(true); // will return false
$stringType->isAssignableValue('text'); // will return true

// Other types: array, bool, callable, iterable, null, resource, void

// Object | Class types

$objectType = \Jojo1981\PhpTypes\AbstractType::createFromTypeName('object');
$objectType->isAssignableValue(true); // will return false
$objectType->isAssignableValue(new \stdClass()); // will return true
$objectType->isAssignableValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntity()); // will return true (for all object types)

$classType1 = \Jojo1981\PhpTypes\AbstractType::createFromValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntity());
$classType1->isAssignableValue(new \stdClass()); // will return false
$classType1->isAssignableValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntity()); // will return true

$classType2 = \Jojo1981\PhpTypes\AbstractType::createFromTypeName(\Jojo1981\PhpTypes\TestSuite\Fixture\TestEntity::class);
$classType2->isAssignableValue(new \stdClass()); // will return false
$classType2->isAssignableValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntityBase); // will return false
$classType2->isAssignableValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntity()); // will return true

$classType3 = \Jojo1981\PhpTypes\AbstractType::createFromTypeName(\Jojo1981\PhpTypes\TestSuite\Fixture\InterfaceTestEntity::class);
$classType3->isAssignableValue(new \stdClass()); // will return false
$classType3->isAssignableValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntityBase); // will return true
$classType3->isAssignableValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntity()); // will return true

// Pseudo types
$mixedType = \Jojo1981\PhpTypes\AbstractType::createFromTypeName('mixed');
$mixedType->isAssignableValue(null); // will return true (always)
$mixedType->isPseudoType(); // will return true

$multiType = \Jojo1981\PhpTypes\AbstractType::createFromTypes([$stringType, $integerType]);
$multiType->isAssignableValue(true); // will return false
$multiType->isAssignableValue('text'); // will return true
$multiType->isAssignableValue(28); // will return true
$multiType->isPseudoType(); // will return true
bash
git clone https://github.com/jojo1981/php-types.git
bash
composer