PHP code example of lc5 / from-array

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

    

lc5 / from-array example snippets


use Lc5\FromArray\FromArrayTrait;

class ExampleClass
{
    use FromArrayTrait;

    private bool $bool;
    private int $int;
    private float $float;
    private string $string;
    private array $array;
    private object $object;
}

$properties = [
    'bool' => true,
    'int' => 2,
    'float' => 3.5,
    'string' => 'example string',
    'array' => ['example array'],
    'object' => new stdClass()
];

$exampleObject = ExampleClass::fromArray($properties);

use Lc5\FromArray\FromArrayTrait;

class ExampleClass
{
    use FromArrayTrait;
    
    /** @var callable */
    private $callable;
    /** @var mixed[] */
    private iterable $iterable;
    /** @var stdClass[] */
    private array $typedArray;
    /** @var stdClass[] */
    private iterable $typedIterable;
    /** @var mixed */
    private $mixed;
    /** @var int|float */
    public $intOrFloat;
}

$properties = [
    'callable' => function (): void {},
    'iterable' => new ArrayObject(),
    'typedArray' => [new stdClass(), new stdClass()],
    'typedIterable' => new ArrayObject([new stdClass(), new stdClass()]),
    'mixed' => 'mixed',
    'intOrFloat' => 1.5
];

$exampleObject = ExampleClass::fromArray($properties);

ExampleClass::fromArray($properties, Options::DEFAULT & ~Options::VALIDATE_REDUNDANT);