Skip to content

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.

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
LibraryPerformanceFeatures
LiteDto~2.8μsEssential features, high performance
SimpleDto #[UltraFast]~2.4μsFast mode with limited features
SimpleDto Normal~16.4μsFull features with validation

LiteDto is ~5.9x faster than SimpleDto Normal while providing essential Dto features.

LibraryPerformanceFeatures
Plain PHP~0.242μsNo features, manual work
Other Dtos~3.83μsMinimal features, maximum speed
LiteDto #[UltraFast]~0.7μsMinimal overhead, maximum speed
SimpleDto #[UltraFast]~2.4μsFast mode with limited features

LiteDto #[UltraFast] is ~25x faster than SimpleDto Normal and only ~2.7x slower than Plain PHP!

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"}

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

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,
]);

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]

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 hidden

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
]);

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',
],
]);

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],
],
]);

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.

  • 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)
  • 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