PHP code example of xactsystems / cast

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

    

xactsystems / cast example snippets




declare(strict_types=1);

namespace Xact\Cast;

use DateTime;
use Xact\Cast\Cast;

class Test
{
    protected int $myInt;
    protected ?int $myNullInt;
    protected float $myFloat;
    protected ?float $myNullFloat;
    protected string $myString;
    protected ?string $myNullString;
    protected bool $myBool;
    protected ?bool $myNullBool;
    protected ?array $myNullArray;
    protected ?object $myNullObject;
    protected ?DateTime $myNullDateTime;

    public function testCast(mixed $value, mixed $nullValue = null): void
    {
        $this->myInt = Cast::intval($value);
        $this->myNullInt = Cast::nullInt($nullValue);
        $this->myFloat = Cast::nullFloat($value);
        $this->myNullFloat = Cast::nullFloat($nullValue);
        $this->myString = Cast::strval($value);
        $this->myNullString = Cast::nullString($nullValue);
        $this->myBool = Cast::boolval($value);
        $this->myNullBool = Cast::nullBool($nullValue);
        $this->myNullArray = Cast::nullArray($nullValue);
        $this->myNullObject = Cast::nullObject($nullValue);
        $this->myNullDateTime = Cast::nullDateTime($nullValue);
    }
}