定制 abdelrhmansaid/container 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

abdelrhmansaid/container

Composer 安装命令:

composer require abdelrhmansaid/container

包简介

Minimal, Lightweight, and Fast PHP Dependency Injection Container.

README 文档

README

tests

Minimal, Lightweight, and Fast PHP Dependency Injection Container

Installation

composer require abdelrhmansaid/container

Testing

composer test

Usage

The methodologies of the container are based on the Dependency Injection paradigm, using the ReflectionClass to get class dependencies.

To use the container, you must first create a new instance of the container.

use AbdelrhmanSaid\Container\Container;

$container = new Container();

Or just use the static method Container::getInstance(), that will return the globally available container if it exists, or create a new one.

$container = Container::getInstance();

After you have created the container, you can bind your dependencies to the container.

$container->bind(Foo::class);

Also, you can create a singleton, that will be returned every time you call the get method.

$container->singleton('foo', function () {
    // ...
});

Singletons are useful for classes that are expensive to instantiate, but only need to be created once.

To get a dependency from the container, you can call the make method.

$foo = $container->make('foo');

The main difference between make and get is that make accepts a second parameter, which is an array of parameters to pass to the constructor of the class, while get does not because of implementing PSR-11

By the way you can also create an alias for a class, so you can call it with a different name.

$container->alias(Foo::class, 'bar');

Auto-wiring

Don't worry about the auto-wiring, the container will do it for you.

$container->make(BrandNewClass::class);

It will automatically bind the dependencies of the class, and if the class has a constructor, it will pass the dependencies to it, also the container can inject specific method dependencies using the call method.

$container->call([Foo::class, 'setBar'], ['bar' => $bar]);

And that's it! Enjoy ✌.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固