devmakerlab / entities
与您的服务或存储库中的实体一起工作。
v3.1.2
2022-02-20 01:36 UTC
Requires
- php: ^7.4|^8.0
Requires (Dev)
- phpunit/phpunit: ^8.9|^9.0
- symfony/var-dumper: ^5.0|^6.0
This package is auto-updated.
Last update: 2024-09-20 07:10:02 UTC
README
此包提供了一种实现实体的方法。对您的服务或存储库非常有用。
用法
在专用类中创建您的实体
use DevMakerLab\Entity; class Human extends Entity { public string $name; public int $age; }
然后以这种方式实例化一个新的实体
$human = new Human([ 'name' => 'Bob', 'age' => 42, ]); echo $human->name; // Bob echo $human->age; // 42
以这种方式创建您的实体列表
use DevMakerLab\EntityList; class People extends EntityList { public function getYoungest(): Human { $entities = $this->entities; uasort($entities, function ($a, $b) { if ($a->age === $b->age) { return 0; } return ($a > $b) ? -1 : 1; }); return array_pop($entities); } }
然后以这种方式实例化一个列表
$bob = new Human(['name' => 'Bob', 'age' => 45]); $junior = new Human(['name' => 'Junior', 'age' => 21]); $jane = new Human(['name' => 'Jane', 'age' => 31]); $people = new People([$bob, $junior]); $people[] = $jane; echo $people[0]->name; // Bob echo $people[1]->name; // Junior echo $people->getYoungest()->name; // Junior