PHP Classes

File: examples/simpleTypes.php

Recommend this page to a friend!
  Classes of Mateo   PHP JSON Decoder into an Object   examples/simpleTypes.php   Download  
File: examples/simpleTypes.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP JSON Decoder into an Object
Decode JSON into an object of a given class
Author: By
Last change:
Date: 4 months ago
Size: 487 bytes
 

Contents

Class file image Download
<?php

use Mateodioev\Json\JSON;

require
__DIR__.'/../vendor/autoload.php';


// Single class example
class User {
    public
int $id;
    public
string $name;
    public
string $username;
}

// JSON string
$rawJson = '{
    "id": 1,
    "name": "John Doe",
    "username": "johndoe"
}'
;

$u = new User;

// Decode JSON string to User object
try {
   
JSON::new($rawJson)->decode($u);
} catch (\
Mateodioev\Json\JsonDecodeException|ReflectionException $e) {
}


var_dump($u);