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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wubinworks/magic-object 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Fast-prototyping magic accessors trait for Doctrine entities
PHP Magic Constants, even more powerful than an Enum
Eloquent extension providing ability to use a lot of subquery functions like fromSubquery or leftJoinSubquery
JSONPath implementation for querying and updating JSON objects with support for json flattening into a table
Generate Doctrine ORM entities.
The InstantConfigurationCopy module provides easy way to copy configuration field information for admin in back office Magento 2.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: OSL-3.0
- 更新时间: 2026-07-15