PHP code example of eventjet / json

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

    

eventjet / json example snippets


use Eventjet\Json\Json;

enum Status: string {
    case Active = 'active';
    case Inactive = 'inactive';
}

final readonly class User
{
    /**
     * @param list<User> $friends
     */
    public function __construct(
        public string $name,
        public Status $status,
        public array $friends,
        public string|null $email = null,
    ) {}
}

$json = '
    {
        "name": "John",
        "status": "active",
        "friends": [
            {"name": "Jane", "status": "inactive", "friends": []}
        ]
    }
';
$user = Json::decode($json, User::class);

echo $user->name; // John
echo $user->status; // Status::Active
echo $user->friends[0]->name; // Jane
echo $user->friends[0]->status; // Status::Inactive