Download the PHP package bermudaphp/types without Composer
On this page you can find all versions of the php package bermudaphp/types. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bermudaphp/types
More information about bermudaphp/types
Files in bermudaphp/types
Package types
Short Description Runtime type checking and validation library for PHP
License MIT
Informations about the package types
Bermuda Types
Runtime type checking and validation library for PHP 8.4+.
Installation
Features
- Runtime type checking and validation via
Types
class - Support for built-in PHP types and custom classes
- Type assertions with detailed error messages
- Backward compatibility with legacy
enforce()
method - Full PHP 8.4 compatibility with typed constants
- Static analysis friendly (PHPStan Level 9, Psalm Level 1)
- Zero dependencies
- Lightweight and performant
Types Utility
The Types
class provides comprehensive runtime type checking and validation.
Basic Type Checking
Type Assertions
Advanced Type Detection
Class and Interface Validation
Real-world Examples
API Reference
Type Constants
Types::TYPE_ARRAY
- Array typeTypes::TYPE_OBJECT
- Object typeTypes::TYPE_INT
- Integer typeTypes::TYPE_BOOL
- Boolean typeTypes::TYPE_STRING
- String typeTypes::TYPE_RESOURCE
- Resource typeTypes::TYPE_CALLABLE
- Callable typeTypes::TYPE_FLOAT
- Float typeTypes::TYPE_NULL
- Null type
Flag Constants
Types::CALLABLE_AS_OBJECT
- Treat callable objects as objectsTypes::OBJECT_AS_CLASS
- Return class name instead of 'object'
Methods
getType(mixed $value, int $flags = 0): string
Determines the type of a variable.
is(mixed $value, string $expectedType): bool
Checks if the value matches the specified type.
isAny(mixed $value, array $expectedTypes): bool
Checks if the value matches any of the provided types.
assert(mixed $value, string|array $expectedTypes, ?string $message = null): void
Asserts that the value matches one of the allowed types.
enforce(mixed $value, string|array $expectedTypes, ?string $message = null): void
Alias for assert() method (backward compatibility).
assertNotNull(mixed $value, string|array $expectedTypes, ?string $message = null): mixed
Asserts that the value is not null and matches one of the allowed types.
isClass(mixed $value, ?string $expectedClass = null): bool
Checks if the value is a valid class name.
isInterface(mixed $value, ?string $expectedInterface = null): bool
Checks if the value is a valid interface name.
isSubclassOf(mixed $value, string $parentClass): bool
Checks if the value is a subclass of the specified class.
isInstanceOf(mixed $value, string $className): bool
Checks if the value is an instance of the specified class or interface.
License
MIT License. See LICENSE file for details.