PHP code example of cupcoffee / reify

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

    

cupcoffee / reify example snippets



use Reify\Reify;

$person = Reify::json($data)->to(Person::class);



use Reify\Attributes\Construct;
use Reify\Attributes\Type;

class Person {
    public string $name;
    
    #[Construct]
    public Profession $profession;
    
    #[Type(Person::class)]
    public array $colleagues;
    
    public ?Person $spouse;
    
    public object $meta;
}




class Person {
    /**
     * Using PHP 8 syntax we can define a type for each property
     * Reify supports all scalar values and custom types defined by you
     */
    public string $name;
    
    
    /**
     * If you have a single value that needs to be mapped to a type
     * You can use the Construct attribute. This will call the constructor with the value instead of mapping it.
     */
    #[Construct]
    public Profession $profession;
    
    /**
     * Unfortunately PHP does not have Generics.
     * We can however still define a type using the Type attribute.
 *   * Reify will try to map each value in the list to the given value
     */
    #[Type(Person::class)]
    public array $colleagues;

    /**
    * Not sure if the data is available? 
    * Reify will take your nullables in to account. 
    */    
    public ?Person $spouse;
    
    /**
    * You don't know what the data is going to look like? 
    * Reify supports mapping to plain objects aswell. 
    */
    public object $meta;
}