定制 radutopala/mnapoli-php-di 二次开发

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

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

radutopala/mnapoli-php-di

Composer 安装命令:

composer require radutopala/mnapoli-php-di

包简介

PHP-DI is a Container that makes Dependency Injection as practical as possible in PHP

README 文档

README

PHP-DI is a Container that makes Dependency Injection as practical as possible.

PHP-DI also tries to avoid falling into the trap of the "Service Locator" antipattern and help you do real dependency injection.

Features

  • Simple to start with
  • Supports different configuration alternatives to suit every taste:
    • Reflection: zero configuration, intelligent guessing
    • Annotations: modern, practical and simple
    • PHP code: if you like complete control and auto-completion
    • PHP array: allows you to store it in a configuration file
    • YAML: elegant and concise
    • XML (work in progress): more verbose, but more classic
  • Performances: supports a large number of Caches
  • Offers lazy injection: lazy-loading of dependencies (WIP)
  • Supports constructor injection, setter injection and property injection

Usage

Let's go to the Getting started guide!

And there is a complete documentation waiting for you.

What is dependency injection, and why use PHP-DI

You can first read the introduction to dependency injection with an example.

Dependency injection and DI containers are separate notions, and one should use of a container only if it makes things more practical (which is not always the case depending on the container you use).

PHP-DI is about this: make dependency injection more practical.

How classic PHP code works

Here is how a code not using DI will roughly work:

  • Application needs Foo (e.g. a controller), so:
  • Application creates Foo
  • Application calls Foo
    • Foo needs Bar (e.g. a service), so:
    • Foo creates Bar
    • Foo calls Bar
      • Bar needs Bim (a service, a repository, …), so:
      • Bar creates Bim
      • Bar does something

How Dependency Injection works

Here is how a code using DI will roughly work:

  • Application needs Foo, which needs Bar, which needs Bim, so:
  • Application creates Bim
  • Application creates Bar and gives it Bim
  • Application creates Foo and gives it Bar
  • Application calls Foo
    • Foo calls Bar
      • Bar does something

This is the pattern of Inversion of Control. The control of the dependencies is inversed from one being called to the one calling.

The main advantage: the one at the end of the caller chain is always you. So you can control every dependencies: you have a complete control on how your application works. You can replace a dependency by another (one you made for example).

For example that wouldn't be so easy if Library X uses Logger Y and you have to change the code of Library X to make it use your logger Z.

How code using PHP-DI works

Now how does a code using PHP-DI works:

  • Application needs Foo so:
  • Application gets Foo from the Container, so:
    • Container creates Bim
    • Container creates Bar and gives it Bim
    • Container creates Foo and gives it Bar
  • Application calls Foo
    • Foo calls Bar
      • Bar does something

In short, PHP-DI takes away all the work of creating and injecting dependencies.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-06-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固