Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/esler/smart-dto

Simple trait for creating "smart" data-transfer-objects in PHP
https://github.com/esler/smart-dto

dto php

Last synced: 2 days ago
JSON representation

Simple trait for creating "smart" data-transfer-objects in PHP

Awesome Lists containing this project

README

        

# smart-dto
Simple trait for creating \"smart\" data-transfer-objects in PHP

## Installation
```
composer require intraworlds/smart-dto
```

## Usage
```php
_config = json_decode($config, true); // loading from DB
} else {
$this->_config = $config;
}
}
}

$user = new UserDTO;
$user->id = 123;
$user->username = 'joe.doe';
$user->config = '{"foo":"bar"}';

// undefined properties will fail
$user->surname = 'doe';
echo $user->surname; // throws an exception

// you can extract an array from DTO
print_r($user);
// > Array(id => 123, username => joe.doe, config => Array(foo => bar))

// you can hydrate object from an array
$user->hydrate(['id' => 456, 'config => ['foo' => 'baz']]);

// the MAIN usage will be probably when loading from DB
$stmt = $pdo->query('SELECT * FROM user', \PDO::FETCH_CLASS, UserDTO::class);
$user = $stmt->fetch();

```

## License
MIT