Skip to content
Search
Examples

Types

Description

Type validation checks if a value matches the expected type without throwing exceptions. The isValid() method returns true if the value conforms to the type, false otherwise. This is useful for conditional logic, filtering data, or performing pre-flight checks before processing.

Unlike assertions, validation never throws - it's designed for control flow decisions rather than enforcing contracts.


Validate PHP resource values. Returns true for valid open resources only.

Documentation

Code

<?php

declare(strict_types=1);

use function Flow\Types\DSL\type_resource;

require __DIR__ . '/vendor/autoload.php';

$handle = fopen('php://memory', 'r+');

echo 'Is file handle valid? ' . (type_resource()->isValid($handle) ? 'yes' : 'no') . "\n";
echo 'Is string valid? ' . (type_resource()->isValid('hello') ? 'yes' : 'no') . "\n";
echo 'Is object valid? ' . (type_resource()->isValid(new \stdClass()) ? 'yes' : 'no') . "\n";

fclose($handle);
echo 'Is closed handle valid? ' . (type_resource()->isValid($handle) ? 'yes' : 'no') . "\n";
Contributors

Built in the open.

Join us on GitHub
scroll back to top