woutvw/nested-object

A PHP class that allows creating nested objects with the flexibility of JSON but the advantages of PHP classes.

v1.0.0 2024-09-26 05:50 UTC

This package is not auto-updated.

Last update: 2025-04-25 06:56:51 UTC


README

Latest Stable Version Total Downloads License

A PHP class that allows you to create nested objects with the flexibility of JSON objects, but with the advantages of PHP classes.

Installation

Install the package via Composer:

composer require woutvw/nested-object

Usage

This class allows you to dynamically create nested objects without predefined structures, similar to working with JSON in JavaScript, but maintaining the advantages of PHP.

Creating a Nested Object

You can create an object with nested properties like this:

use WoutVW\NestedObject;

// Initialize with data
$data = [
    'user' => [
        'name' => 'John Doe',
        'email' => 'john@example.com'
    ],
    'status' => 'active'
];

$object = new NestedObject($data);

// Access nested properties
echo $object->user->name; // Outputs 'John Doe'

// Set new properties
$object->user->age = 30;

Setting values

You can assign new values just like a regular object:

$object->user->city = 'New York';

Converting to an Array

You can convert the nested object back to an array using the toArray() method:

$array = $object->toArray();
print_r($array);

License

MIT