aedart/overload
最新稳定版本:5.1.0
Composer 安装命令:
composer require aedart/overload
包简介
Provides means to dynamically deal with inaccessible properties, by implementing PHP's magic methods; __get(), __set(), __isset(), and __unset(). This package, however, enforces the usage of getters- and setters-methods, ensuring that if a property is indeed available, then its corresponding getter
README 文档
README
Deprecated - Overload
Package has been replaced by aedart/athenaeum
Provides means to dynamically deal with inaccessible properties, by implementing PHP's magic methods;
__get(), __set(), __isset(), and __unset(). This package, however, enforces the usage of getters- and setters-methods, ensuring that if a property
is indeed available, then its corresponding getter or setter method will be invoked. The term 'overload', in this context, refers to PHP’s own definition hereof. (http://php.net/manual/en/language.oop5.overloading.php)
Contents
- When to use this
- How to install
- Quick start
- Naming convention applied
- Protected vs. Private properties
- Custom usage
- Contribution
- Versioning
- License
When to use this
Generally speaking, magic methods can be very troublesome to use. For the most part, they prohibit the usage of auto-completion in IDEs and if not documented, developers are forced to read large sections of the source code, in order to gain understanding of what’s going on. Depending upon implementation, there might not be any validation, when dynamically assigning new properties to objects, which can break other components, which depend on the given object. In addition to this, it can also be very difficult to write tests for components that are using such magic methods.
This package will not be able to solve any of the mentioned problems, because at the end of the day, as a developer, you still have to ensure that the code readable / understandable, testable and documented. Therefore, I recommend that this package only to be used, if and only if, the following are all true;
- Properties shouldn't be allowed to be dynamically created and assigned to an object, without prior knowledge about them. Thus, properties must always be predefined.
- Getters and setters must always be used for reading / writing properties
- You wish to allow access to an object’s properties like such:
$person->age;and still enforce some kind of validation.
How to install
composer require aedart/overload
This package uses composer. If you do not know what that is or how it works, I recommend that you read a little about, before attempting to use this package.
Quick start
A Typical Class
<?php use Aedart\Overload\Traits\PropertyOverloadTrait; /** * @property string $name Name of a person */ class Person { use PropertyOverloadTrait; /** * Name of a person * * @var string|null */ protected $name = null; // This is made accessible, because of the PropertyOverloadTrait. // When attempted to read, getName() is invoked // When attempted to write, setName($value) is invoked // A getter method for $name public function getName() : string { return $this->name; } // A setter method $name public function setName(string $value) { if( ! empty($value)){ $this->name = $value; return; } throw new InvalidArgumentException('Provided name is invalid'); } } // Some place else, in your application, you can then invoke the following: $person = new Person(); $person->name = 'Alin'; // Invokes the setName(...) echo $person->name; // Invokes the getName(), then outputs 'Alin' echo isset($person->name); // Invokes the __isset(), then outputs true unset($person->name); // Invokes the __unset() and destroys the name property
Tip: PHPDoc
When using PHP’s magic methods, for overloading properties, it is a very good idea to make use pf PHPDoc's @property-tag.
Most IDEs can read it and make use of it to provide auto-completion.
See http://www.phpdoc.org/docs/latest/references/phpdoc/tags/property.html
Naming convention applied
Property names
This package assumes that you properties either follow CamelCase or Snake Case standard. Consider the following examples:
<?php $personId = 78; // Valid $category_name = 'Products'; // Valid $swordFish_length = 63; // Invalid, because its a mix of both camelCase and underscore
Getter / Setter names
Getters and setters follow a most strict naming convention; the must follow camelCase and be prefixed with get for getter methods and set for setter methods. In addition, the package uses the following syntax / rule when searching for a property’s corresponding getter or setter:
getterMethod = getPrefix, camelCasePropertyName;
getPrefix = "get";
setterMethod = setPrefix, camelCasePropertyName;
setPrefix = "set";
camelCasePropertyName = {uppercaseLetter, {lowercaseLetter}};
uppercaseLetter = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K"
| "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X"
| "Y" | "Z" ;
lowercaseLetter = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k"
| "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x"
| "y" | "z" ;
Above stated syntax / rule is expressed in EBFN
Examples:
<?php $personId = 78; // Will look for getPersonId() and setPersonId($value); $category_name = 'Products'; // Will look for getCategoryName() and setCategoryName($value);
Protected vs. Private properties
By default, only protected properties are going to be made accessible (or overloaded, if you will). This means that private declared properties are not going to be available.
<?php use Aedart\Overload\Traits\PropertyOverloadTrait; class Person { use PropertyOverloadTrait; protected $name = null; // This is made accessible, because of the PropertyOverloadTrait. // When attempted to read, getName() is invoked // When attempted to write, setName($value) is invoked private $age = null; // Is NOT made accessible, read / write attempts will result in Exception // Remaining implementation not shown... }
Behaviour override
Should you wish to also expose your private declared properties, then this behaviour can be set per object, from an inside scope.
<?php use Aedart\Overload\Interfaces\PropertyAccessibilityLevel; use Aedart\Overload\Traits\PropertyOverloadTrait; class Person { use PropertyOverloadTrait; protected $name = null; // This is made accessible, because of the PropertyOverloadTrait. // When attempted to read, getName() is invoked // When attempted to write, setName($value) is invoked private $age = null; // This is made accessible, because of the $this->setPropertyAccessibilityLevel(...). // When attempted to read, getName() is invoked // When attempted to write, setName($value) is invoked public function __construct(){ // Change the property accessibility to private $this->setPropertyAccessibilityLevel(PropertyAccessibilityLevel::PRIVATE_LEVEL); } }
For further reference, read documentation in Overload/Traits/Helper/PropertyAccessibilityTrait
Custom usage
If you do not need the full property overload methods, e.g. you only wish to be able to get and set, but not to unset properties, then you can make use of the sub-traits, which the PropertyOverloadTrait is composed of.
However, when using individually, you must also include the Aedart\Overload\Traits\Helper\ReflectionTrait or the individual traits are not going to work as expected; A fatal error will be thrown, stating that it cannot find various methods!
Each of the below listed traits can be used by themselves, without being dependent on each other.
| Trait | Description | Namespace |
|---|---|---|
| ReflectionTrait | Must always be included | Aedart\Overload\Traits\Helper\ReflectionTrait |
| GetterInvokerTrait | Implements __get() |
Aedart\Overload\Traits\GetterInvokerTrait |
| SetterInvokerTrait | Implements __set() |
Aedart\Overload\Traits\SetterInvokerTrait |
| IssetInvokerTrait | Implements __isset() |
Aedart\Overload\Traits\IssetInvokerTrait |
| UnsetInvokerTrait | Implements __unset() |
Aedart\Overload\Traits\UnsetInvokerTrait |
Contribution
Have you found a defect ( bug or design flaw ), or do you wish improvements? In the following sections, you might find some useful information on how you can help this project. In any case, I thank you for taking the time to help me improve this project's deliverables and overall quality.
Bug Report
If you are convinced that you have found a bug, then at the very least you should create a new issue. In that given issue, you should as a minimum describe the following;
- Where is the defect located
- A good, short and precise description of the defect (Why is it a defect)
- How to replicate the defect
- (A possible solution for how to resolve the defect)
When time permits it, I will review your issue and take action upon it.
Fork, code and send pull-request
A good and well written bug report can help me a lot. Nevertheless, if you can or wish to resolve the defect by yourself, here is how you can do so;
- Fork this project
- Create a new local development branch for the given defect-fix
- Write your code / changes
- Create executable test-cases (prove that your changes are solid!)
- Commit and push your changes to your fork-repository
- Send a pull-request with your changes
- Drink a Beer - you earned it :)
As soon as I receive the pull-request (and have time for it), I will review your changes and merge them into this project. If not, I will inform you why I choose not to.
Versioning
This package follows Semantic Versioning 2.0.0
License
BSD-3-Clause, Read the LICENSE file included in this package
aedart/overload 适用场景与选型建议
aedart/overload 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.5k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2014 年 11 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「property」 「Overload」 「Magic Method」 「__set」 「__get」 「__isset」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 aedart/overload 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aedart/overload 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 aedart/overload 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Fast-prototyping magic accessors trait for Doctrine entities
PHP Magic Constants, even more powerful than an Enum
Utility library for making class that can contain properties
Eloquent extension providing ability to use a lot of subquery functions like fromSubquery or leftJoinSubquery
A Dotenv extension to use multiple environment config files
PHP Utils for ninjas, warlocks and code-monkeys
统计信息
- 总下载量: 12.5k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 26
- 依赖项目数: 3
- 推荐数: 6
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2014-11-25