Introduction to LiteDto
LiteDto is an ultra-fast, minimalistic Data Transfer Object (Dto) library for PHP 8.2+ that provides essential features with maximum performance.
What is LiteDto?
Section titled “What is LiteDto?”LiteDto is designed for developers who need:
- ✅ Maximum Performance - 7.6x faster than SimpleDto Normal
- ✅ Minimal Overhead - Only essential features, no validation, no pipeline
- ✅ Simple API - Easy to learn and use
- ✅ Attribute-Driven - Clean, declarative syntax
- ✅ Optional Converter - JSON/XML support when needed
Performance Comparison
Section titled “Performance Comparison”Standard Mode
Section titled “Standard Mode”| Library | Performance | Features |
|---|---|---|
| LiteDto | ~2.8μs | Essential features, high performance |
| SimpleDto #[UltraFast] | ~2.4μs | Fast mode with limited features |
| SimpleDto Normal | ~16.4μs | Full features with validation |
LiteDto is ~5.9x faster than SimpleDto Normal while providing essential Dto features.
UltraFast Mode
Section titled “UltraFast Mode”| Library | Performance | Features |
|---|---|---|
| Plain PHP | ~0.242μs | No features, manual work |
| Other Dtos | ~3.83μs | Minimal features, maximum speed |
| LiteDto #[UltraFast] | ~0.7μs | Minimal overhead, maximum speed |
| SimpleDto #[UltraFast] | ~2.4μs | Fast mode with limited features |
LiteDto #[UltraFast] is ~25x faster than SimpleDto Normal and only ~2.7x slower than Plain PHP!
Quick Example
Section titled “Quick Example”use event4u\DataHelpers\LiteDto\LiteDto;use event4u\DataHelpers\LiteDto\Attributes\MapFrom;use event4u\DataHelpers\LiteDto\Attributes\Hidden;
class UserDto extends LiteDto{ public function __construct( public readonly string $name, public readonly int $age,
#[MapFrom('email_address')] public readonly string $email,
#[Hidden] public readonly string $password, ) {}}
// Create from array$user = UserDto::from([ 'name' => 'John Doe', 'age' => 30, 'email_address' => 'john@example.com', 'password' => 'secret123',]);
// Serialize (password is hidden)$array = $user->toArray();// ['name' => 'John Doe', 'age' => 30, 'email' => 'john@example.com']
$json = $user->toJson();// {"name":"John Doe","age":30,"email":"john@example.com"}UltraFast Mode
Section titled “UltraFast Mode”For maximum performance, use the #[UltraFast] attribute:
use event4u\DataHelpers\LiteDto\LiteDto;use event4u\DataHelpers\LiteDto\Attributes\UltraFast;
#[UltraFast]class ProductDto extends LiteDto{ public function __construct( public readonly string $name, public readonly float $price, public readonly int $stock, ) {}}
// ~0.8μs per operation (20x faster than SimpleDto Normal!)$product = ProductDto::from([ 'name' => 'Laptop', 'price' => 999.99, 'stock' => 10,]);UltraFast Mode Trade-offs:
- ✅ ~0.8μs per operation (only ~4.8x slower than Plain PHP)
- ✅ Direct property assignment (minimal overhead)
- ❌ No attribute processing (
#[MapFrom],#[MapTo],#[Hidden], etc.) - ❌ No nested DTOs or collections
- ❌ No enum support or custom casters
Use UltraFast when:
- Maximum performance is critical
- Simple flat DTOs without nesting
- No special attribute features needed
Core Features
Section titled “Core Features”1. Property Mapping
Section titled “1. Property Mapping”Map properties from different source keys:
class ProductDto extends LiteDto{ public function __construct( #[From('product_name')] public readonly string $name,
#[From('product_price')] public readonly float $price, ) {}}
$product = ProductDto::from([ 'product_name' => 'Laptop', 'product_price' => 999.99,]);2. Output Mapping
Section titled “2. Output Mapping”Map properties to different target keys when serializing:
use event4u\DataHelpers\LiteDto\Attributes\MapTo;
class UserDto extends LiteDto{ public function __construct( #[MapTo('full_name')] public readonly string $name,
#[MapTo('user_age')] public readonly int $age, ) {}}
$user = UserDto::from(['name' => 'John', 'age' => 30]);$array = $user->toArray();// ['full_name' => 'John', 'user_age' => 30]3. Hidden Properties
Section titled “3. Hidden Properties”Exclude sensitive properties from serialization:
class UserDto extends LiteDto{ public function __construct( public readonly string $name,
#[Hidden] public readonly string $password,
#[Hidden] public readonly string $apiKey, ) {}}
$user = UserDto::from([ 'name' => 'John', 'password' => 'secret', 'apiKey' => 'key123',]);
$array = $user->toArray();// ['name' => 'John'] - password and apiKey are hidden4. Convert Empty to Null
Section titled “4. Convert Empty to Null”Automatically convert empty strings and arrays to null:
use event4u\DataHelpers\LiteDto\Attributes\ConvertEmptyToNull;
class UserDto extends LiteDto{ public function __construct( public readonly string $name,
#[ConvertEmptyToNull] public readonly ?string $middleName,
#[ConvertEmptyToNull] public readonly ?array $tags, ) {}}
$user = UserDto::from([ 'name' => 'John', 'middleName' => '', // Converted to null 'tags' => [], // Converted to null]);5. Nested DTOs
Section titled “5. Nested DTOs”Automatically hydrate nested DTOs:
class AddressDto extends LiteDto{ public function __construct( public readonly string $street, public readonly string $city, ) {}}
class UserDto extends LiteDto{ public function __construct( public readonly string $name, public readonly AddressDto $address, ) {}}
$user = UserDto::from([ 'name' => 'John', 'address' => [ 'street' => '123 Main St', 'city' => 'New York', ],]);6. Collections
Section titled “6. Collections”Handle arrays of DTOs:
class TeamDto extends LiteDto{ public function __construct( public readonly string $name, /** @var array<UserDto> */ public readonly array $members, ) {}}
$team = TeamDto::from([ 'name' => 'Engineering', 'members' => [ ['name' => 'John', 'age' => 30], ['name' => 'Jane', 'age' => 25], ],]);7. Converter Mode (Optional)
Section titled “7. Converter Mode (Optional)”Enable JSON/XML support with the #[ConverterMode] attribute:
use event4u\DataHelpers\LiteDto\Attributes\ConverterMode;
#[ConverterMode]class UserDto extends LiteDto{ public function __construct( public readonly string $name, public readonly int $age, ) {}}
// Now accepts JSON$user = UserDto::from('{"name":"John","age":30}');
// And XML$user = UserDto::from('<root><name>John</name><age>30</age></root>');Note: ConverterMode adds ~0.5μs overhead but enables multiple input formats.
When to Use LiteDto?
Section titled “When to Use LiteDto?”✅ Use LiteDto When:
Section titled “✅ Use LiteDto When:”- You need maximum performance with essential Dto features
- You want simple, clean code without complex validation
- You need property mapping and serialization
- You want nested DTOs and collections
- Performance is critical (APIs, high-traffic applications)
❌ Use SimpleDto Instead When:
Section titled “❌ Use SimpleDto Instead When:”- You need validation (Required, Email, Min, Max, etc.)
- You need custom casts (DateTime, Enum, etc.)
- You need computed properties or lazy loading
- You need conditional properties
- You need pipeline processing
Next Steps
Section titled “Next Steps”- Creating LiteDtos - Learn how to create and use LiteDtos
- Attributes Reference - Complete guide to all attributes
- Performance Tips - Optimize your LiteDtos