PHP code example of jamielsharief / data-transfer-object

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

    

jamielsharief / data-transfer-object example snippets


use DataTransferObject\DataTransferObject;

class Employee extends DataTransferObject
{
    public string $name;
    public string $email;
    public ?Employee $reportsTo;
    public int $age;
    public bool $active = true;

    /**
     * @var \App\DataTransferObject\Employee[] $subordinates
     */
    public array $subordinates = [];
}

$employee = new Employee();
$employee->name = 'sarah';


 $sarah = new Employee([
            'name' => 'Sarah',
            'email' => '[email protected]',
            'reportsTo' => $claire
        ]);

class Employee extends DataTransferObject
{
    public string $name;
    public string $email;
    public ?Employee $reportsTo;

    /**
     * @var \App\DataTransferObjects\Employee[] $subordinates
     */
    public array $subordinates = [];
}

 $employee = Employee::fromArray([
    'name' => 'Sarah',
    'email' => '[email protected]',
    'reportsTo' => [
        'name' => 'Claire',
        'email' => '[email protected]',
    ],
    'subordinates' => [
        [
            'name' => 'Jon',
            'email' => '[email protected]'
        ]
    ]
]);

$employee->toArray();

$employee->toString();

$employee = Contact::fromString(
    '{"name":"Jon","company":"Snow Enterprises","email":"[email protected]","age":33,"unsubscribed":false}'
);