stubbles/ioc 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

stubbles/ioc

Composer 安装命令:

composer require stubbles/ioc

包简介

Dependency injection.

README 文档

README

Dependency injection.

Build status

Tests

Latest Stable Version Latest Unstable Version

Installation

stubbles/ioc is distributed as Composer package. To install it as a dependency of your package use the following command:

composer require "stubbles/ioc": "^12.0"

Requirements

stubbles/ioc requires at least PHP 8.3.

Inversion of Control

stubbles/ioc provides a very simple-to-use but still powerful inversion of control container, which supports constructor and setter based dependency injection. The IoC container of stubbles/ioc is modeled after Google Guice and makes use of type hinting annotations. If you've never heard of type hinting or annotations, you should at first read the sections on these two topics:

The example code

Imagine, you are building a car configurator. To follow the rules of good design, you define interfaces for all components of a car and provide several classes that implement these components.

The interfaces in you application include:

interface Car {
    public function moveForward($miles);
}
interface Person {
    public function sayHello();
}
interface Tire {
    public function rotate();
}
interface Engine {
    public function start();
}

The implementations are:

class BMW implements Car {
    public function __construct(private Engine $engine, private Tire $tire, private Person $driver) {
    }
    public function moveForward($miles) {
        $this->driver->sayHello();
        $this->engine->start();
        $this->tire->rotate();
    }
}

class Schst implements Person {
    public function sayHello() {
        echo "My name is Stephan\n";
    }
}

class Goodyear implements Tire {
    public function rotate() {
        echo "Rotating Goodyear tire\n";
    }
}

class TwoLitresEngine implements Engine {
    public function start() {
        echo "Starting 2l engine\n";
    }
}

Without the dependency injection framework

To create a new instance of an implementation of Car the following code is required:

    $tire   = new Goodyear();
    $engine = new TwoLitresEngine();
    $schst  = new Schst();

    $bmw    = new BMW($engine, $tire, $schst);
    $bmw->moveForward(50);

Creating objects manually like this has several drawbacks:

  • Your application is bound to the concrete implementations instead of the interfaces
  • Changing the implementation means changing existing code, which might break it
  • The creation of objects is scattered throughout your application

Of course, real applications have a lot more classes, so things only get worse then.

Enter 'Inversion of Control'

stubbles/ioc tries to solve these problems by providing functionality to handle all dependency injections for you. This keeps your application clean of boilerplate code.

Furthermore, it allows you to centralize and/or modularize the definition of the concrete implementations for your interfaces or abstract types.

A simple example

To define the concrete implementations is done using an instance of stubbles\ioc\Binder:

$binder = new \stubbles\ioc\Binder();
$binder->bind('Car')->to('BMW');
$binder->bind('Tire')->to('Goodyear');
$binder->bind('Person')->to('Schst');
$binder->bind('Engine')->to('TwoLitresEngine');

In this short code snippet, you bound the interfaces from the example above to their concrete implementations.

If you now need an instance of the engine, you use the binder to create a stubbles\ioc\Injector, which can be used to create the desired Engine:

$injector = $binder->getInjector();
$engine = $injector->getInstance('Engine');
var_dump($engine);

This code snippet will now display:

object(TwoLitresEngine)#48 (0) {
}

As desired, it created an instance of the concrete implementation, that you bound to the interface.

Next, you probably want to get an instance of Car using the same approach:

    $injector = $binder->getInjector();
    $car = $injector->getInstance('Car');
    var_dump($car);
object(BMW)#33 (3) {
  ["driver:private"]=>
  NULL
  ["engine:private"]=>
  object(TwoLitresEngine)#37 (0) {
  }
  ["tire:private"]=>
  object(Goodyear)#40 (0) {
  }
}

stubbles/ioc created a new instance of BMW, as you bound it to Car, and as the constructor of BMW requires a Tire and an Engine instance, it created these instances as well. To determine the concrete classes to use, stubbles/ioc used the bindings you defined in the stubbles\ioc\Binder instance.

What you also can see is, that Stubbles did not inject an object into the $driver property, although you specified a binding for Person. stubbles/ioc will never inject any dependencies via setter methods.

Further features

stubbles/ioc 适用场景与选型建议

stubbles/ioc 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.61k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2016 年 01 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 stubbles/ioc 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 stubbles/ioc 我们能提供哪些服务?
定制开发 / 二次开发

基于 stubbles/ioc 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2016-01-03