am2tec / hydrator
使用类设置器来给你的对象补水
1.0
2023-05-11 15:55 UTC
Requires
- php: ^7.0|^8.1
Requires (Dev)
- phpunit/phpunit: ~6.5.13
This package is auto-updated.
Last update: 2024-09-11 19:04:36 UTC
README
Hydrator
PHP 库,用于使用类设置器给对象补水
安装
composer require am2tec/hydrator
如何使用
如果你有一个这样的类
class Example
{
private $test;
public function setTest($test)
{
$this->test = $test;
}
public function getTest()
{
return $this->test;
}
}
并且需要从一个数组创建一个对象
$myData = [
'test' => 'this is a test'
];
你可以使用 FerFabricio\Hydrate\Hydrate
以简单的方式完成这项工作
use FerFabricio\Hydratate\Hydrate;
class Example
{
private $test;
public function setTest($test)
{
$this->test = $test;
}
public function getTest()
{
return $this->test;
}
}
$myData = [
'test' => 'this is a test'
];
$example = Hydratate::toObject(Example::class, $myData);
echo $example->getTest(); // This line will print: this is a test
自动加载
这个库只兼容 PSR-4,请确保你的应用程序已经正确配置以使用这个库。