PHP code example of damianulan / php-dtos

1. Go to this page and download the library: Download damianulan/php-dtos 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/ */

    

damianulan / php-dtos example snippets


use DTOs\Dto;

class UserDto extends Dto
{
    // here you can define default values to your attributes
    protected $attributes = [
        'name' => 'Damian',
        'email' => '[email protected]',

    ];

    // here you can define attributes that are assignable outside of constructor
    // then assigning anything outside of this list will throw an exception
    protected $fillable = ['name', 'email'];


    /**
     * Here contain all the logic you need.
    **/
}

$dto = new UserDto();

// or with attributes assigned.
// these will be assigned to the object before its initialization
$dto = new UserDto([
    'name' => 'Damian',
    'email' => '[email protected]',
]);

// you can retrieve attributes changes as:
$dto->name = 'Alexander';
$dto->getDirty('name'); // returns 'Alexander'
$dto->getOriginal('name'); // returns 'Damian'

// those will return all attributes as an assoc array
$dto->all();
$dto->toArray();

// check if attributes are empty/filled
$dto->isEmpty();
$dto->isFilled();

// or check if a specific attribute is assigned
$dto->hasAttribute('name');
$dto->hasAttribute('email');

use DTOs\Workshop\ReadOnlyAttributes;

/**
 * This interface will prevent overriding attributes over those previously assigned.
 */
class UserDto extends Dto implements ReadOnlyAttributes
{
    //
}

composer