PHP code example of vanengers / php-json-object-library

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

    

vanengers / php-json-object-library example snippets


class SamplePhpObject extends PhpJsonObject 
{
    public string $property = 'value';
    
    public function setProperty(string $data): self
    {
        $this->property = $value;
        // do stuff here like parsing data, transformation or validations
        
        return $this;
    }
}

$object = new SamplePhpObject("{\"property\":\"new value\"}");
$array = $object->toArray(['skip' => ['property']]);
$json = $object->toJson(['skip' => ['property']]);

$object = new SamplePhpObject("{\"property\":\"new value\"}");
$array = $object->toArray(['skip' => ['property']]);
$json = $object->toJson(['skip' => ['property']]);

$object = new SamplePhpObject("{\"property\":\"new value\"}");
$array = $object->toArray(['skip_null' => ['property']]);
$json = $object->toJson(['skip_null' => ['property']]);

$object = new SamplePhpObject("{\"property\":\"new value\"}");
$array = $object->toArray(['skip_empty' => ['property']]);
$json = $object->toJson(['skip_empty' => ['property']]);

class SamplePhpObject extends PhpJsonObject 
{
    public $mappers = [
           'person_name' => 'author',
           'recipient_name' => 'recipient',
    ];

    public string $author = '';
    public string $recipient = '';
    
    public function setPerson($data) 
    {
        if (is_array($data)) {
            $this->fromArray($data);
        }
    }
    
    public function setRecipient($data) 
    {
        if (is_array($data)) {
            $this->fromArray($data);
        }
    }
}