PHP code example of vakazona / dto

1. Go to this page and download the library: Download vakazona/dto library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vakazona / dto example snippets


use vakazona\Dto\DTO;

class TestDTO extends DTO
{
    public string $name;
    
    public ?string $lastName;
    
    public string|int $age;
    
    public bool $developer = true;
}

$data = new TestDTO([
    'name' => 'Valery',
    'age' => 23,
]);

$data->name; // Valery
$data->age; // 23
$data->developer; // true

use vakazona\Dto\DTO;
use vakazona\Dto\Attributes\Required;

class TestDTO extends DTO
{
    #[Required]
    public string $price;
}

$data = new TestDTO([]);

use vakazona\Dto\DTO;
use vakazona\Dto\Attributes\Flexible;

#[Flexible]
class TestDTO extends DTO
{
    public string $name;
}

$data = new TesDTO([
    'name' => 'Valery',
    'age' => 23,
]);

$data->toArray(); // ['name' => 'Valery', 'age' => '23'];

use vakazona\Dto\DTO;

class CustomPropertyDTO extends DTO
{
    public string $name;

    public int $age;

}

use vakazona\Dto\DTO;

class TestCustomDTO extends DTO
{
    public CustomPropertyDTO $customProperty;
}


$data = new TestCustomDTO([
            'customProperty' => new CustomPropertyDTO([
                'name' => 'Valera',
                'age' => 23
            ])
        ]);