PHPackages                             wubinworks/magic-object - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. wubinworks/magic-object

ActiveLibrary

wubinworks/magic-object
=======================

Object with Magic Getter and Setter.

1.0.1(1mo ago)031OSL-3.0PHPPHP &gt;=5.3

Since Jul 15Pushed 1mo agoCompare

[ Source](https://github.com/wubinworks/magic-object)[ Packagist](https://packagist.org/packages/wubinworks/magic-object)[ Docs](https://www.wubinworks.com)[ RSS](/packages/wubinworks-magic-object/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (2)Dependencies (2)Versions (3)Used By (1)

Object with Magic Getter and Setter
===================================

[](#object-with-magic-getter-and-setter)

The `MagicObject` class is an excellent choice for manipulating structured data. For example, configuration data represented by `JSON`, `XML`, `YML`, etc.

The `MagicObject` can also be used for representing a database entry and the class extending it is often called `Model`.

```
$obj->setSomething(123);
$obj->getSomething(); // 123
```

The Magic Getter and Setter can make your code extremely simple and straightforward. For detailed usage, see the [full example](#full-example) section.

Requirements
------------

[](#requirements)

- php &gt;= 5.3

Installation
------------

[](#installation)

```
composer require wubinworks/magic-object
```

Usage
-----

[](#usage)

### Data path naming

[](#data-path-naming)

The `MagicObject` stores nested array data and those nested array keys can be represented by "path".

*The key's separator is `/`*. The best practice to name a path is to use lowercase letters, digits and underscores for keys only and the keys should begin with a lowercase letter.

For instance, `this/is/a/path`. The keys are `this`, `is`, `a` and `path`.

```
/** @var \Wubinworks\MagicObject\Data\MagicObject $obj */
$obj->setData('this/is/a/path', 'some value');
$obj->getData('this/is/a/path'); // some value
```

### Path and method name conversion

[](#path-and-method-name-conversion)

Here is the ***Magic***. The path can be converted to Getter and Setter method name.

```
/** @var \Wubinworks\MagicObject\Data\MagicObject $obj */
$obj->getData()
$obj->get()

$obj->setData(, $value)
$obj->set($value)

$obj->hasData()
$obj->has()

$obj->unsData()
$obj->uns()
```

The above example demonstrates how `snake_case` path is converted to `PascalCase` partial method name, and vice versa.

*Note the key's separator `/` is converted to `_`, and vice versa.*

For instance, `customer/reward_points/expired` &lt;==&gt; `Customer_RewardPoints_Expired`.

*You may still be confused, so see the full example below.*

### Full example

[](#full-example)

```
$data = json_decode('{
    "customer": {
        "name": "John Smith",
        "gender": null,
        "age": 25,
        "reward_points": {
            "used": 1000,
            "expired": 2000
        }
    },
    "order_number": 111111111
}', true);
/*
$data = [
    'customer' => [
        'name' => 'John Smith',
        'gender' => null,
        'age' => 25,
        'reward_points' => [
            'used' => 1000,
            'expired' => 2000
        ];
    ],
    'order_number' => 11111111
];
*/

// Feed the object with data. Array with numeric keys are *NOT* supported.
$obj = new \Wubinworks\MagicObject\Data\MagicObject($data);

// Getter

$obj->getData('order_number'); // 11111111
$obj->getOrderNumber(); // 11111111

$obj->getData('customer/reward_points/used'); // 1000
$obj->getCustomer_RewardPoints_Used(); // 1000

// If the path does not exist, null will be returned and no exception will be thrown.
$obj->getData('customer/nonexist_key'); // null
$obj->getCustomer_NonexistKey(); // null

// Get all data
$obj->getData(/** No path parameter */); // $data

// Setter

$obj->setData('order_number', 22222222);
$obj->getData('order_number'); // 22222222
$obj->setOrderNumber(33333333);
$obj->getData('order_number'); // 33333333

$obj->setData('customer/reward_points/used', 7777);
$obj->getData('customer/reward_points/used'); // 7777
$obj->setCustomer_RewardPoints_Used(8888);
$obj->getData('customer/reward_points/used'); // 8888
$obj->setData('customer/new_key', 9999);
$obj->getData('customer/new_key'); // 9999

// Replace all data
// Actually, it is Setter. Note the path parameter of setData is null.

$arr = ['a' => 1, 'b' => 2];
$obj->setData(null, $arr);
$obj->getData(); // $arr

// Has path

$obj->setData(null, $data);
$obj->hasData('customer/gender'); // true
$obj->hasCustomer_Gender(); // true
$obj->hasData('customer/nonexist_key'); // false

// Unset path ("uns" means "unset")
// Always succeeds, even if the path does not exist.
// Note the path parameter of unsData cannot be null and must be string

$obj->unsData('customer/gender');
$obj->hasData('customer/gender'); // false
$obj->unsCustomer_Age();
$obj->hasData('customer/age'); // false

$obj->unsData('customer/nonexist_key');
$obj->unsCustomer_NonexistKey();
```

Unit testing
------------

[](#unit-testing)

Install the `require-dev` dependencies and run the following command.

```
vendor/bin/phpunit
```

♥
-

[](#)

If you like this package or this package helped you, please ***share*** and ***give a ★☆star☆★***, it's NOT hard!

###  Health Score

34

—

LowBetter than 74% of packages

Maintenance92

Actively maintained with recent releases

Popularity4

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity30

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~5 days

Total

2

Last Release

41d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7de965a6287fb784969afeb4b173521d3cb59c6b873b7248263abb9fc098eddd?d=identicon)[wubinworks](/maintainers/wubinworks)

---

Top Contributors

[![wubinworks](https://avatars.githubusercontent.com/u/127310257?v=4)](https://github.com/wubinworks "wubinworks (3 commits)")

---

Tags

gettermagic-objectphpsetterpathgettersettermagicmagic gettermagic setter

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/wubinworks-magic-object/health.svg)

```
[![Health](https://phpackages.com/badges/wubinworks-magic-object/health.svg)](https://phpackages.com/packages/wubinworks-magic-object)
```

###  Alternatives

[usmanhalalit/get-set-go

Dynamic Setter-Getter for PHP 5.4+

1913.8k1](/packages/usmanhalalit-get-set-go)[antares/accessible

PHP library that allows you to define your class' getters, setters and constructor with docblock annotations.

123.9k1](/packages/antares-accessible)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
