neovg/php-struct
Composer 安装命令:
composer require neovg/php-struct
包简介
A "C struct"-like class with type safe attributes and fluent setter interface.
关键字:
README 文档
README
A "C struct"-like class with type safe attributes and fluent setter interface.
Synopsis
/**
* @property int $foo
* @property string $bar
*
* @method $this withFoo(int $value)
* @method $this withBar(string $value)
*/
class DataStruct extends StructAbstract
{
}
$data = DataStruct::createFromJson(
'{"foo":1,"bar":"something"}'
);
$data = DataStruct::createFromArray([
'foo' => 1,
'bar' => 'something',
]);
$data = (new DataStruct())
->withFoo(1)
->withBar('something');
$data = new DataStruct();
$data->foo = 1;
$data->bar = 'something';
echo $data->foo; // '1'
echo $data->bar; // 'something'
echo (string)$data; // '{"foo":1,"bar":"something"}'
Description
A convenient way to create strictly typed classes for arbitrary data structures without having to write lots of getters and setters. For even more convenience, setters can be chained.
Additionally, a cast to string of an instantiated object returns JSON!
How it works
StructAbstract reads the @property-read annotations and creates an internal representation of them, which is then used by the magic methods call() and get() to emulate strictly types setters and readable properties.
Available Data Types for Properties
- bool
- int
- double
- string
- array
- object
- callable
Storing Objects
When storing objects in Structs, the type in the annotations must be the actual class (or some superclass of it), because arguments passed to the setter will be checked with instanceof.
Default Values
The default value of each property is null. You can override this by adding a private or protected property of the same name with a default value.
/**
* @method WithDefaultStruct someproperty(int $value)
*
* @property-read string $someproperty
*/
class WithDefaultStruct extends StructAbstract
{
protected $someproperty = 'default value';
}
$data = new WithDefaultStruct();
echo $data->someproperty; // 'default value'
Array Properties
/**
* @property string[] $strings
* @property ChildStruct[] $childs
*/
class ArrayStruct extends StructAbstract
{
}
$array = ArrayStruct::createFromArray([
'strings' => ['foo', 'bar'],
'childs' => [
['property' => 'value1'],
['property' => 'value2'],
],
]);
echo $array->strings[0]; // 'foo'
echo $array->childs[0]->property; // 'value1'
neovg/php-struct 适用场景与选型建议
neovg/php-struct 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14.69k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 02 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「datastructure」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 neovg/php-struct 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 neovg/php-struct 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 neovg/php-struct 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
QuadTree implementation in PHP
PHP Classes for Trie datastructures
An implementation of the Directed Acyclic Graph (DAG) for Laravel models
DataTree: Tree data-structure component for Nette Framework
JS Map/Set DataStructure implementation for PHP
统计信息
- 总下载量: 14.69k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 26
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2019-02-16