serafim/properties
Composer 安装命令:
composer require serafim/properties
包简介
PHP properties implementation
README 文档
README
PHP Properties implementations based on getters or setters method and used PSR-5 PHPDoc information.
Installation
composer require serafim/properties
Introduction
Properties provide the ability to control the behavior of setting and retrieving data from an object fields.
There are no properties at the language level, but they can be implemented with the some additional functionality.
For example:
class MyClass { protected $a = 23; public function __get($field) { return $this->$field; } } $dto = new MyClass(); echo $dto->a; // 23 $dto->a = 42; // Cannot access protected property MyClass::$a
This code example does not provide type-hint and autocomplete. Using magic without the ability to control it is always a bad option. ...well, it looks awful %)
We can fix some problems using PSR-5. In this case, we can add a docblock.
/** * @property-read int $a */ class MyClass { // ...
But this docblock only adds information for the IDE and does not affect the code itself. At the same time, you should always keep it up to date.
But wait! We have another problem.
$dto = new MyClass(); echo isset($dto->a); // false
This is obviously a bug. We still need to add __isset, __unset and __set support.
It's damn awful!!111
Properties Usage
Now let's see what the serafim/properties package provides.
/** * @property $a */ class MyClass { use Serafim\Properties\Properties; protected $a = 23; } $dto = new MyClass(); $dto->a; // 23 $dto->a = 42; // Ok $dto->a; // 42
Readonly Properties
To indicate that the type should be readonly use @property-read annotation.
/** * @property-read $a */ class MyClass { use Serafim\Properties\Properties; protected $a = 23; } $dto = new MyClass(); $dto->a; // 23 $dto->a = 42; // Error: Property MyClass::$a is readonly
Writeonly Properties
To indicate that the type should be readonly use @property-write annotation.
/** * @property-write $a */ class MyClass { use Serafim\Properties\Properties; protected $a = 23; } $dto = new MyClass(); $dto->a = 42; // 42 $dto->a; // Error: Property MyClass::$a is writeonly
Getters And Setters
For getter or setter override just declare get[Property]
(is[Property] for bool values will be works too) or set[Property] methods.
/** * @property-read int $a */ class MyClass { use Serafim\Properties\Properties; protected $a = 23; protected function getA() { return $this->a + 19; } } $dto = new MyClass(); echo $dto->a; // 42 (because 23 + 19 = 42) $dto->a = 42; // Error: Property is read-only (@property-read doc declaration)
Setter:
/** * @property-write string $anotherProperty */ class Some { // ... protected $anotherProperty = 'some'; /** * @param string $newVal */ public function setAnotherProperty($newVal) { // Just example if (mb_strlen($newVal) > 4) { throw new InvalidArgumentException('...'); } $this->anotherProperty = $newVal; } }
Autocomplete
All these annotations fully work in the IDE, including autocomplete and highlighting incorrect behavior.
Type Hints
All properties with writeable behavior will be "type checkable".
/** * @property int|null $a */ class Some { use Serafim\Properties\Properties; protected $a; } // $some = new Some; $some->a = 23; // Ok $some->a = null; // Ok $some->a = 'string'; // Error: "TypeError: Value for property Some::$a must be of the type int|null, string given"
Primitive Type Hints
int(orinteger) - property value are integerbool(orboolean) - value are booleanfloat(ordouble) - value are floatstring- value are string or object with__toStringmethodnull(orvoid) - value are nullableresource- value are resourceobject- value can be any objectmixed- no type checkingcallable- value can be string, instance of \Closure, array with 2 args or object with__invokemethodscalar- value cannot be an objectcountable- value can be a countable (array or object providedCountableinterface).self- value can be object of self class or string with name of self class
selfkeyword does not available yet: it will be supports in future
static- value can be instance of self class or string whos are sublass of self
statickeyword does not available yet: it will be supports in future
$this- value can be only object instance of self class
$thiskeyword does not available yet: it will be supports in future
Arrays And Generics
array- value is type of arrayClass[]- value is type of array or instance of \Traversablescalar[]- value is type of array or instance of \TraversableCollection<>- value is type of array or instance of "Collection" and \TraversableCollection<T>- value is type of array or instance of "Collection" and \TraversableCollection<T,V>- value is type of array or instance of "Collection" and \Traversable
Conjunction And Disjunction
a|b- means that the value must be type(a or b).a&b- means that the value must be type(a and b).a|b&c- means that the value must be type(a or (b and c)).
See more: https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#appendix-a-types
Production Mode
The code is quite effective, but in the production mode you should use caching. The package implements support for the PSR-16 standard.
$driver = new Psr16CacheDriver(); // Your PSR16 cache driver implementation $properties = Serafim\Properties\Bootstrap::getInstance(); $properties->setCacheDriver($driver);
serafim/properties 适用场景与选型建议
serafim/properties 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.12k 次下载、GitHub Stars 达 77, 最近一次更新时间为 2016 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「properties」 「type-hint」 「fields」 「psr-5」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 serafim/properties 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 serafim/properties 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 serafim/properties 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple and fast implementation of enumerations with native PHP
Utility library for making class that can contain properties
Compatibility layer for emulating enumerations in PHP < 8.1 and native enumerations in PHP >= 8.1
Trait providing methods to set class properties with an array.
Register advanced custom fields with object oriented PHP
A behavior for standard fields logic in CRUD based on kartik dynagrid and detailView
统计信息
- 总下载量: 9.12k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 77
- 点击次数: 12
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-04-14