PHP code example of postfriday / castable

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

    

postfriday / castable example snippets


use Postfriday\Castable\Traits\ToArray;
use Postfriday\Castable\Attributes\CastWith;
use Postfriday\Castable\Casters\DateTimeCaster;
use Postfriday\Castable\Casters\BooleanCaster;

class Example
{
    use ToArray;

    public DateTimeInterface $createdAt;
    public DateTimeInterface $updatedAt;
    public bool $isActive;

    public function __construct(
        #[CastWith(DateTimeCaster::class, ['d.m.Y H:i'])] 
        DateTimeInterface $createdAt,

        #[CastWith(DateTimeCaster::class, ['Y-m-d H:i:s'])]
        DateTimeInterface $updatedAt,

        #[CastWith(BooleanCaster::class)]
        bool $isActive
    ) {
        $this->createdAt = $createdAt;
        $this->updatedAt = $updatedAt;
        $this->isActive = $isActive;
    }
}

$example = new Example(
    new \DateTime('2024-02-21 12:00:00'),
    new \DateTime('2025-02-21 18:30:00'),
    true
);

print_r($example->toArray());