Skip to content

Latest commit

 

History

History
148 lines (122 loc) · 4.51 KB

Examples.md

File metadata and controls

148 lines (122 loc) · 4.51 KB

Examples

Mutable

Using basic.dto.xml.

In these cases the DTO is just an explicit transport tool, and it is OK/expected that the DTO changes over its lifetime, and code will set, add, remove from it. Values are also often expected to be not set.

Most importantly: Copies of such an entity will cause side-effects to the original(s). This fact of mutability must be known to you as programmer to avoid bugs in your software.

$ownerDto = new OwnerDto();
$ownerDto->setName('The Owner');

$carDto = new CarDto();
$carDto->setOwner($ownerDto);

$otherCarDto = $carDto;

// A trivial example
$otherCarDto->getOwner()->setName('The new owner');

// You might not expect the original $carDto to also change it's value... Hopefully you do :)
$this->assertSame('The new owner', $otherCarDto->getOwner()->getName());
$this->assertSame('The new owner', $carDto->getOwner()->getName());

Immutable

Using immutable.dto.xml.

In these cases we want to make sure, the DTO, once created, is not changing anymore that easily. Required values are for sure set. Modifications create a new object, keeping the previous one unchanged for further use with that dataset.

$array = [
    'id' => 2,