PHP code example of bluestone / dto

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

    

bluestone / dto example snippets


use Bluestone\DataTransferObject\DataTransferObject;

class Hooman extends DataTransferObject
{
    public string $name;
}

$jane = new Hooman(name: 'Jane');
$john = new Hooman(['name' => 'John']);

use Bluestone\DataTransferObject\DataTransferObject;
use Bluestone\DataTransferObject\Attributes\CastWith;
use Bluestone\DataTransferObject\Casters\ArrayCaster;

class Hooman extends DataTransferObject
{
    public string $name;
    
    #[CastWith(ArrayCaster::class, type: Hooman::class)]
    public array $children;
}

$jane = new Hooman(
    name: 'Jane', 
    children: [
        new Hooman(name: 'Mario'), 
        new Hooman(name: 'Luigi'),
    ],
);

$john = new Hooman([
    'name' => 'John',
    'children' => [
        ['name' => 'Mario'],
        ['name' => 'Luigi'],
    ],
]);

use Bluestone\DataTransferObject\DataTransferObject;
use Bluestone\DataTransferObject\Attributes\Map;

class Hooman extends DataTransferObject
{
    #[Map('date_of_birth')]
    public string $bornAt;
}

$jane = new Hooman(
    date_of_birth: '1970-01-01', 
);

$john = new Hooman([
    'date_of_birth' => '1970-01-01',
]);