zloynick/joole-containers

v8.1.3 2022-04-26 11:35 UTC

This package is not auto-updated.

Last update: 2024-09-25 20:12:26 UTC


README

Joole Containers 是一个用于存储对象的库。在核心上,它是 PSR-12 的实现及其补充。

入门指南

  • 使用 composer 安装
composer require zloynick/joole-containers

使用方法

  • 创建容器对象

use joole\containers\Container;

$container = new Container();
...

  • 将对象注册到已创建的容器中
...
class MyCustomPeople{

    private string $age;
    private string $name;

    public function __construct(
        $name,
        $age
    )
    {
        $this->name = $name;
        $this->age = $age;
    }

    public function getName(): string
    {
        return $this->name;
    }

}

$container->register(MyCustomPeople::class, ['name' => 'Alex', 'age' => 22]);
...

现在我们可以从容器中使用对象


...
echo $container->get(MyCustomPeople::class)->getName();// Alex
...

附加功能

  • 你可以获取容器中的对象数量:$count = count($container);
  • 多重推送

$container->multiplePush(['param1' => [1, 2, 3], 'param2' => new stdClass()], Class1::class, Class2::class);

注意: 参数数组的键必须与构造函数类变量的名称对应。