Warning: You are browsing the documentation for PrestaShop 1.7, which is outdated.
You might want to read an updated version of this page for the current version, PrestaShop 8. Read the updated version of this page
PrestaShop is using Data Transfer Objects (or DTOs) in its codebase, in this section you learn how you can benefit from using DTOs as well.
Data Transfer Object is object (e.g. UserData
) that contains data in a structured format. This means that DTO usually comes with getter methods or public properties for accessing it’s data. The main benefit of using DTOs is that you know what data it has.
For example, if DTO has method getName()
you can be sure that name is there, unlike using arrays, when you never know if key name
exists inside an array or not. By making effective use of DTOs you are also making your code more readable and maintainable.
Benefits of using DTOs:
PrestaShop extensively uses DTOs in it’s QueryHandlers
(you can learn more about it in CQRS section). As an example, you can take a look at GetCustomerForEditingHandlerInterface
below.
<?php
namespace PrestaShop\PrestaShop\Core\Domain\Customer\QueryHandler;
use PrestaShop\PrestaShop\Core\Domain\Customer\QueryResult\EditableCustomer;
use PrestaShop\PrestaShop\Core\Domain\Customer\Query\GetCustomerForEditing;
interface GetCustomerForEditingHandlerInterface
{
/**
* @param GetCustomerForEditing $query
*
* Handler returns EditableCustomer DTO instead of unstructured array,
* thus allowing developer to know exactly what data can be accessed.
*
* @return EditableCustomer
*/
public function handle(GetCustomerForEditing $query);
}
Now imagine you are using implementation of GetCustomerForEditingHandlerInterface
handler in your code.
<?php
use PrestaShop\PrestaShop\Core\Domain\Customer\QueryResult\EditableCustomer;
/** EditableCustomer $editableCustomer */
$editableCustomer = $handler->handle($query);
// You don't need to add additional assertions to see if customer's first name exists
// as DTO explicity provides you it via getter method.
$editableCustomer->getFirstName();