natsumeaurlia/auto-property-reflection

自动反射属性。

1.2 2022-01-31 17:45 UTC

This package is auto-updated.

Last update: 2024-09-16 09:20:20 UTC


README

提供简单的属性初始化。API 资源可以轻松表示。

安装

composer require natsumeaurlia/auto-property-reflection

用法

继承自抽象类。定义所需的属性名称。这必须与您接受的键的名称相同。

class GithubUser extends PropertyReflector
{
    public $id;
}

$user = new GithubUser(['id' => 'xxxx']);
$user->id;

如果属性未定义,则不会设置。

class GithubUser extends PropertyReflector
{
    public $id;
}

$user = new GithubUser(['name' => 'xxxx']);
$user->name; // access undefined property

示例

class GithubUser extends PropertyReflector
{
    public $name;
}

// cURL,Guzzle ...etc
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://api.github.com/users/octocat');
$user = new GithubUser($res->getBody()->getContents());
$user->name; // octocat