PHP code example of dnt / json

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

    

dnt / json example snippets


use DNT\Json\Json;

$json = Json::make(string $path, bool $ensure = false, int $mode = 0755, $recursive = true);
// hoặc
$json = new Json(string $path, bool $ensure = false, int $mode = 0755, $recursive = true);

 

    // đặt đường dẫn mới
	public function setPath(string $path): Jsonable;

    // lấy đường dẫn hiện hành
	public function getPath(): string;

    // lấy thuộc tính - nếu null hoặc không tồn tại thì sẽ lấy $default - $default có thể là 1 callable
	public function get(string $key, mixed $default = null): mixed;
    
    // chuyển sang dạng json
	public function toJson(int $flag = JSON_FORCE_OBJECT): string;

    // kiểm tra thuộc tính tồn tại không
	public function has($offset): bool;
    
    // đặt giữ liệu mới
	public function setAttributes(array $attributes = []): Jsonable;
    
    // lấy tất cả thuộc tính
	public function all(): array;
	
	// lưu các thay đổi vào file
	public function save(): bool;
 
use DNT\Json\Json;

$json=Json::make($path);

// Lấy thuộc tính tương tự $json->get('foo');
$json->foo;
 
 // đặt giá trị mới tương tự $json->set('foo','bar');
 $json->foo = 'bar';
 
 // sử dụng anonymous function
 $json->foo = function() {
    return 'bar';
 };
 // hoặc
 $json->set('foo',function(){
    return 'bar';
 });
 // cũng có thể dùng callback để lấy giá trị mặc định
 $json->get('foo',function(){
    return 'bar';
 });