Using DTOs for Safe Data Transport in PHP Projects

Hamid Ghorashi
3 min readFeb 26, 2023

--

What is Data Transfer Object (DTO)

Data Transfer Objects (DTOs) have become an increasingly popular concept in modern software development. They are simple objects that are used to transfer data between different layers of an application. The primary purpose of a DTO is to facilitate communication between different layers of an application by providing a standardized interface that can be used by all components.

Encapsulating multiple related data items

Reducing the number of arguments in functions can bring significant improvements to the code by making it more readable, easier to test, and maintainable. This is where DTOs can assist in addressing this challenge.

For example, consider a method that accepts several parameters related to a user profile, such as first name, last name, email address, and phone number. Instead of passing each parameter individually, a DTO object could be created that encapsulates all of this data, like so:

class UserProfileDto {
public $firstName;
public $lastName;
public $email;
public $phone;
}

function updateUserProfile(UserProfileDto $profile) {
// Update user profile using the data in $profile
}

Although, data encapsulation can provide various benefits for your code, there are still certain aspects that need to be taken into consideration, such as the fact that attempting to access an undefined attribute of a DTO class can result in an exception being thrown.

A Lightweight Library for Data Transport in PHP Applications

The jetcod/data-transport library offers a straightforward and effective means of transferring data within your application. By utilizing Data Transport you can effortlessly establish and control your data structures, guaranteeing that your application’s data is properly arranged and readily manageable.

It helps to enhance the overall reliability to the system by effectively reducing the risk of failure when accessing DTO object attributes.

Similar to any other package, you can install this library to your application using Composer:

composer require jetcod/data-transport

When you install the package, you can define an object with ease by creating an empty class that extends from Jetcod\DataTransport\AbstractDTO.

Back to the previous example we can improve the code as follow:

<?php 

namespace App\DTO;

use Jetcod\DataTransport\AbstractDTO;

class UserProfileDto extends AbstractDTO
{
}

Attributes of the object can be assigned either one by one:

$userProfileDto = new UserProfileDto();

$userProfileDto->firstName = 'John';
$userProfileDto->lastName = 'Doe'
$userProfileDto->email = 'john.doe@example.com';
$userProfileDto->phone = '(555) 123-4567';

Or all at once in a group:

$userProfileDto = new UserProfileDto([
'firstName' => 'John',
'lastName' => 'Doe',
'email' => 'john.doe@example.com',
'phone' => '(555) 123-4567'
];

Then, you can safely transfer the $userProfileDto object across the application while ensuring that the data is consistently organized and reducing the risk of encountering exceptions when calling non-existent attributes. For instance, if you try $userProfileDto->city you will get null:

$city = $userProfileDto->city;  // Returns null

$userProfileDto->has('email'); // Returns true
$userProfileDto->has('city'); // Returns false

Additionally, the object transforms its attributes to Array or Json format by calling toArray() and toJson() methods.

Conclusion

Data Transfer Objects (DTOs) are widely used in modern software development to transfer data between different layers of an application. DTOs can help to encapsulate multiple related data items and reduce the number of arguments in functions, which ultimately leads to more readable, testable, and maintainable code. However, certain aspects of DTOs need to be taken into consideration, such as accessing undefined attributes. To address this, the jetcod/data-transport library provides a lightweight and straightforward solution for transferring data within PHP applications. Using this library, developers can effortlessly establish and control their data structures, guaranteeing that their application’s data is properly arranged and readily manageable.

--

--