定制 wubinworks/magic-object 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wubinworks/magic-object

Composer 安装命令:

composer require wubinworks/magic-object

包简介

Object with Magic Getter and Setter.

README 文档

README

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 section.

Requirements

  • php >= 5.3

Installation

composer require wubinworks/magic-object

Usage

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

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

/** @var \Wubinworks\MagicObject\Data\MagicObject $obj */
$obj->getData(<snake_case path>)
$obj->get<PascalCase partial method name>()

$obj->setData(<snake_case path>, $value)
$obj->set<PascalCase partial method name>($value)

$obj->hasData(<snake_case path>)
$obj->has<PascalCase partial method name>()

$obj->unsData(<snake_case path>)
$obj->uns<PascalCase partial method name>()

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 <==> Customer_RewardPoints_Expired.

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

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

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!

wubinworks/magic-object 适用场景与选型建议

wubinworks/magic-object 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 07 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「path」 「magic」 「getter」 「setter」 「magic getter」 「magic setter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 wubinworks/magic-object 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 wubinworks/magic-object 我们能提供哪些服务?
定制开发 / 二次开发

基于 wubinworks/magic-object 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: OSL-3.0
  • 更新时间: 2026-07-15