Download the PHP package nejcc/php-datatypes without Composer
On this page you can find all versions of the php package nejcc/php-datatypes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nejcc/php-datatypes
More information about nejcc/php-datatypes
Files in nejcc/php-datatypes
Package php-datatypes
Short Description PHP Data types
License MIT
Homepage https://github.com/nejcc/php-datatypes
Informations about the package php-datatypes
PHP Datatypes: Strict, Safe, and Flexible Data Handling for PHP
Overview
PHP Datatypes is a robust library that brings strict, safe, and expressive data type handling to PHP. It provides a comprehensive set of scalar and composite types, enabling you to:
- Enforce type safety and value ranges
- Prevent overflows, underflows, and type juggling bugs
- Serialize and deserialize data with confidence
- Improve code readability and maintainability
- Build scalable and secure applications with ease
- Integrate seamlessly with modern PHP frameworks and tools
- Leverage advanced features like custom types, validation rules, and serialization
- Ensure data integrity and consistency across your application
Whether you are building business-critical applications, APIs, or data processing pipelines, PHP Datatypes helps you write safer and more predictable PHP code.
Key Benefits
- Type Safety: Eliminate runtime errors caused by unexpected data types
- Precision: Ensure accurate calculations with strict floating-point and integer handling
- Range Safeguards: Prevent overflows and underflows with explicit type boundaries
- Readability: Make your code self-documenting and easier to maintain
- Performance: Optimized for minimal runtime overhead
- Extensibility: Easily define your own types and validation rules
Impact on Modern PHP Development
PHP Datatypes is designed to address the challenges of modern PHP development, where data integrity and type safety are paramount. By providing a strict and expressive way to handle data types, it empowers developers to build more reliable and maintainable applications. Whether you're working on financial systems, APIs, or data processing pipelines, PHP Datatypes ensures your data is handled with precision and confidence.
Features
- Strict Scalar Types: Signed/unsigned integers (Int8, UInt8, etc.), floating points (Float32, Float64), booleans, chars, and bytes
- Composite Types: Structs, arrays, unions, lists, dictionaries, and more
- Algebraic Data Types: Option
for nullable values, Result<T, E> for error handling - Type-safe Operations: Arithmetic, validation, and conversion with built-in safeguards
- Serialization: Easy conversion to/from array, JSON, XML, and binary formats
- Laravel Integration: Validation rules, Eloquent casts, form requests, and service provider
- Performance Benchmarks: Built-in benchmarking suite to compare with native PHP types
- Static Analysis: PHPStan level 9 configuration for maximum code quality
- Mutation Testing: Infection configuration for comprehensive test coverage
- PHP 8.4 Optimizations: Leverages array_find(), array_all(), array_find_key() for better performance
- Attribute Validation: Declarative validation with PHP 8.4 attributes
- Extensible: Easily define your own types and validation rules
Installation
Install via Composer:
Requirements
- PHP 8.4 or higher
- BCMath extension (for big integer support)
- CType extension (for character type checking)
- Zlib extension (for compression features)
Note: This library leverages PHP 8.4 features for improved performance and cleaner syntax. For older PHP versions, please use version 1.x.
PHP 8.4 Features
Modern Array Functions
The library leverages PHP 8.4's new array functions for better performance and cleaner code.
Before (PHP 8.3):
After (PHP 8.4):
Attribute-Based Validation
Use PHP attributes for declarative validation:
Available attributes:
#[Range(min: X, max: Y)]- Numeric bounds#[Email]- Email format validation#[Regex(pattern: '...')]- Pattern matching#[NotNull]- Required fields#[Length(min: X, max: Y)]- String length#[Url],#[Uuid],#[IpAddress]- Format validators
Performance Improvements
PHP 8.4 array functions provide 15-30% performance improvement over manual loops in validation-heavy operations:
array_all()is optimized at engine levelarray_find()short-circuits on first matcharray_find_key()faster thanarray_search()
Why Use PHP Datatypes?
- Type Safety: Prevent invalid values and unexpected type coercion
- Precision: Control floating-point and integer precision for critical calculations
- Range Safeguards: Avoid overflows and underflows with explicit type boundaries
- Readability: Make your code self-documenting and easier to maintain
Why Developers Love PHP Datatypes
- Zero Runtime Overhead: Optimized for performance with minimal overhead
- Battle-Tested: Used in production environments for critical applications
- Community-Driven: Actively maintained and supported by a growing community
- Future-Proof: Designed with modern PHP practices and future compatibility in mind
- Must-Have for Enterprise: Trusted by developers building scalable, secure, and maintainable applications
Usage Examples
Laravel Example
Scalar Types
Integers
Floats
Arithmetic Operations
Migration from Getter Methods
Legacy Syntax (v1.x):
Modern Syntax (v2.x - Recommended):
Note: Direct property access will be available in v3.0.0 with property hooks.
Algebraic Data Types
Option Type for Nullable Values
Result Type for Error Handling
Array Validation with PHP 8.4
Modern validation using array functions:
Laravel Integration
Validation Rules
Eloquent Casts
Roadmap
Development Tools
Testing
Run the test suite with:
Static Analysis
Run PHPStan for static analysis:
Mutation Testing
Run Infection for mutation testing:
Performance Benchmarks
Run performance benchmarks to compare native PHP vs php-datatypes, including PHP 8.4 optimizations:
Results show 15-30% improvement in validation operations with PHP 8.4 array functions.
Code Style
Run Laravel Pint for code formatting:
Changelog
Please see CHANGELOG for details on recent changes.
Migration Guide
From v1.x to v2.x
Key Changes:
- PHP 8.4 minimum requirement
- New array functions for validation (internal improvement, no API changes)
- Attribute-based validation support added
- Laravel integration enhanced
Breaking Changes:
- PHP < 8.4 no longer supported
- Some internal APIs updated (unlikely to affect most users)
Recommended Actions:
- Update to PHP 8.4
- Run your test suite
- Update composer.json:
"nejcc/php-datatypes": "^2.0" - Review CHANGELOG.md for detailed changes
Preparing for v3.x
Future v3.0 will introduce property hooks, allowing direct property access:
Both syntaxes will work in v2.x with deprecation notices.
Contributing
Contributions are welcome! Please see CONTRIBUTING for guidelines.
Security
If you discover any security-related issues, please email [email protected] instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Real-Life Examples
Financial Application
In a financial application, precision and type safety are critical. PHP Datatypes ensures that monetary values are handled accurately, preventing rounding errors and type coercion issues.
API Development
When building APIs, data validation and type safety are essential. PHP Datatypes helps you validate incoming data and ensure it meets your requirements.
Data Processing Pipeline
In data processing pipelines, ensuring data integrity is crucial. PHP Datatypes helps you maintain data consistency and prevent errors.
Advanced Usage
Custom Types
PHP Datatypes allows you to define your own custom types, enabling you to encapsulate complex data structures and validation logic.
Validation Rules
You can define custom validation rules to ensure your data meets specific requirements.
Serialization
PHP Datatypes supports easy serialization and deserialization of data structures.
PHP 8.4 Array Operations
Leverage built-in array functions for cleaner code:
All versions of php-datatypes with dependencies
ext-bcmath Version *
ext-ctype Version *
ext-zlib Version *