承接 phico/container 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

phico/container

Composer 安装命令:

composer require phico/container

包简介

Lightweight dependency injection container for Phico

README 文档

README

Small and focussed Dependency Injection container for the Phico framework.

Installation

Using composer

composer require phico/container

Reference

Set a definition describing how to create the class set(string $classname, callable|string $defintion, array $args =[])

Set a shortcut alias on a definition alias(string $name)

Set the share flag on a definition share(bool $toggle)

Returns an instatiated class get(string $classname or $alias)

Usage

// pass an optional array of parameters through the constructor
$c = new Phico\Container([
    'autowiring' => false, // default is true
    'sharing' => false, // default is true
]);

// use get() to fetch an instantiated class
$foo = $c->get(Foo::class);
// foo is now an instance of Foo

Autowiring

With autowiring enabled, Container will do it's best to find and construct the class without any explicit instructions.

When autowiring is disabled, Container will need definitions creating using set()

// set() accepts a string for simple cases
$c->set(Foo::class, Foo::class);

// or callables for more complex requirements
$c->set(Foo::class, function() {
    return new Foo::class
});

// shorter function syntax is fine too
$c->set(Foo::class, fn() => new Foo());

Aliases

Sometimes you just want to avoid typing, you can add an alias using alias() after the call to set();

// set an alias to Foo named Bar, now foo can be accessed by get(Bar)
$c->set(Foo::class, Foo::class)
    ->alias('Bar');
$bar = $c->get('Bar');
// bar is now an instance of Foo

Sharing

Some classes are better when shared, the default is true to always share instances once they have been created, however sometimes you will need an unique instance on every call to get(), toggle this behaviour using the share() method, it accepts a boolean true or false and will only affect the current definition.

$c->set(Foo::class, Foo::class)
    ->share(false);
$a = $c->get(Foo::class);
$b = $c->get(Foo::class);

// $a is not equal to $b, they are different instances

Contributing

Container is considered feature complete, however if you discover any bugs or issues in it's behaviour or performance please create an issue, and if you are able a pull request with a fix.

Please make sure to update tests as appropriate.

For major changes, please open an issue first to discuss what you would like to change.

License

BSD-3-Clause

统计信息

  • 总下载量: 37
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2024-06-23