PHP code example of apfelfrisch / data-transfer-object

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

    

apfelfrisch / data-transfer-object example snippets


use Apfelfrisch\DataTransferObject;
use Apfelfrisch\DataTransferObject\Casters\DateTimeCast;

class MyDTO extends DataTransferObject
{
    public function __construct(
        public int $a,

        public float $b,

        public OtherDTO $otherDTO,
        
        #[DateTimeCast]
        public DateTime $date,
    ) { }
}

$dto = MyDTO::fromArrayWithCast([
    'a' => 1,
    'b' => 2.2,
    'otherDTO' => ['id' => 3],
    'date' => '2021-05-01'
);