php-7 Anonymous class Simple in-place data wrapper

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

interface IArrayWrapper {
    public function getProperties(): array;
    public function has(string $name): bool;
    public function __toString();
    // ... 
};

/**
 * Lightweight in-place data wrapper.
 * Demonstrates usage of anonymous class in conjunction with interface.
 * 
 * Provides some basic functionality for managing array data in OO style.
 * Can be used as a wrapper for API request/response data etc.
 * Converts data to JSON with simple `(string)` cast.
 */
new class($data) implements IArrayWrapper
{
    /** @var array */
    private $data;

    public function __construct(array $data)
    {
        $this->data = $data;
    }
    
    public function getProperties(): array
    {
        return is_array($this->data) ? array_keys($this->data) : [] ;
    }

    public function has(string $name): bool
    {
        return (bool)($this->data[$name] ?? false);
    }

    public function get(string $name)
    {
        return $this->data[$name] ?? null;
    }
    
    public function __isset($name)
    {
        return $this->has($name);
    }
    
    public function __get($name)
    {
        return $this->get($name);
    }
    
    public function __toString()
    {
        return json_encode($this->data);
    }
};

Usage

Assume our $data as follows and class is stored in $cls variable:

$data = ['a' => 'b', 'c' => 'd', 'e' => 5];
$cls->a; // b
$cls->b; // null
$cls->e; // 5
isset($cls->a); // true
isset($cls->b); // false
$cls->has('a'); // true
$cls->has('b'); // false
$cls->getProperties(); // Array([0] => a [1] => c [2] => e)
(string)$cls; // {"a":"b","c":"d","e":5}
$cls instanceof IArrayWrapper; // true


Got any php-7 Question?