PHP Classes

File: readme.MD

Recommend this page to a friend!
  Classes of Mateo   PHP JSON Decoder into an Object   readme.MD   Download  
File: readme.MD
Role: Documentation
Content type: text/markdown
Description: Documentation
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: 904 bytes
 

Contents

Class file image Download

JSON decoder

Decode a json string into a class

First step

composer require mateodioev/jsondecoder

use Mateodioev\Json;

Usage

Create a class with public atributes, egg:

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

Create a new instance of JSON class with json raw

$jsonRaw = '{"id": 1, "name": "Mateo", "username": "mateodioev"}';
$json = new JSON($jsonRaw);

Decode content

$u = new User;

$json->decode($u)

Now var $u containts content of $jsonRaw

var_dump($u);

example output:

class User#2 (3) {
  public int $id =>
  int(1)
  public string $name =>
  string(5) "Mateo"
  public string $username =>
  string(10) "mateodioev"
}

Exceptions

Mateodioev\Json\JsonDecodeException