sportlog/di 问题修复 & 功能扩展

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

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

sportlog/di

Composer 安装命令:

composer require sportlog/di

包简介

A simple PSR-11 dependency injection container

README 文档

README

A simple PSR-11 dependency injection container.

Install via Composer

You can install sportlog/di using Composer.

$ composer require sportlog/di

Minimum PHP version required is 8.

How to use

<?php

require 'vendor/autoload.php';

use Sportlog\DI\Container;

// Given this class and interface:
interface FooInterface
{
    public function getFoo(): string;
}

class Foo implements FooInterface
{
    public function __construct(private ?string $foo = 'foo')
    {
    }

    public function getFoo(): string
    {
        return $this->foo;
    }
}

// 1) You can simply get the instance via class id
$container = new Container();
$foo = $container->get(Foo::class);
echo $foo->getFoo();    // outputs: "foo"

// 2) When working with interfaces you must set
// the class id which shall be created
$container = new Container();
$container->set(FooInterface::class, Foo::class);
$foo = $container->get(FooInterface::class);
echo $foo->getFoo();    // outputs: "foo"

// 3) You can also use a factory. Parameters are
// getting injected
class Config
{
    public function getFooInit(): string
    {
        return 'init-foo';
    }
}

$container = new Container();
// Instance of Config will be injected to the factory
$container->set(
    FooInterface::class,
    fn (Config $config) => new Foo($config->getFooInit())
);
$foo = $container->get(FooInterface::class);
echo $foo->getFoo();    // outputs: "init-foo"

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-12-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固