rakshazi/get-set-trait
Composer 安装命令:
composer require rakshazi/get-set-trait
包简介
Dynamic Setter-Getter for PHP 5.4+
README 文档
README
A dynamic setter-getter library for PHP 5.4+.
You can use methods like setFoo('bar') and getFoo(), which you DON'T have to create (in your class).
GetSetTrait will make these methods work for you automatically as long as you have a $foo property in your class.
It makes use of Traits, so using it is super simple, you don't have to extend any class, as you can extend a single class only, we don't force you to use ours.
You can restrict to only getter only or you can specify a Type for property using annotations.
Installation
GetSetTrait uses Composer to make hassles Go.
Learn to use composer and add this to require (in your composer.json):
"rakshazi/get-set-trait": "@stable"
Library on Packagist.
Usage
Just add this in your classes:
use Rakshazi\GetSetTrait;
class Dummy { //Add ability to use dynamic getters and setters use \Rakshazi\GetSetTrait; } //Init dummy class $dummy = new Dummy; //Set new var 'message_for_world' with value $dummy->setMessageForWorld('Hello'); //Will return "Hello\n" echo $dummy->getMessageForWorld()."\n"; //Will return the same text echo $dummy->getData('message_for_world')."\n"; //Set new message for our world! $dummy->setData('message_for_world', 'Bye-bye!'); //Will return "Bye-bye!\n" echo $dummy->getData('message_for_world')."\n"; //Will set new var 'new_message' $dummy->setNewMessage('Use me now!'); //Will return true, value exists $dummy->hasNewMessage(); //Will return false $dummy->hasSomeOtherValue(); //Will show all object data var_dump($dummy);
That's basically it.
Advanced usage
Data property
If you want save all data in $object->someProperty array instead of saving each property as object's property ($object->property_name),
you can use setDataProperty('data') function, example:
<?php class Dummy { //Add ability to use dynamic getters and setters use \Rakshazi\GetSetTrait; public function __construct() { $this->setDataProperty('data'); } } //Init dummy class $dummy = new Dummy; //Set new var 'message_for_world' with value $dummy->setMessageForWorld('Hello'); //Will return "Hello\n" echo $dummy->getMessageForWorld()."\n"; //Will return the same text echo $dummy->getData('message_for_world')."\n"; //Set new message for our world! $dummy->setData('message_for_world', 'Bye-bye!'); //Will return "Bye-bye!\n" echo $dummy->getData('message_for_world')."\n"; //Will set new var 'new_message' $dummy->setNewMessage('Use me now!'); //Will return all properties in an array $dummy->getAllData(); //Will show all object data var_dump($dummy);
Result (all data saved in data property)
object(Dummy)#1 (2) {
["_data_property":"Dummy":private]=>
string(4) "data"
["data"]=>
array(2) {
["message_for_world"]=>
string(8) "Bye-bye!"
["new_message"]=>
string(11) "Use me now!"
}
}
rakshazi/get-set-trait 适用场景与选型建议
rakshazi/get-set-trait 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.72k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2016 年 05 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「trait」 「getter」 「setter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rakshazi/get-set-trait 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rakshazi/get-set-trait 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rakshazi/get-set-trait 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generate Doctrine ORM entities.
Trait providing methods to set class properties with an array.
Expansion traits for Yii2 components
Traits to build collections of specific objects
Get private object attributes directly, without calling getter methods.
KISS implementation of RequestTrait with usage of CURL
统计信息
- 总下载量: 11.72k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 24
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-05-27