itlessons/php-ioc 问题修复 & 功能扩展

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

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

itlessons/php-ioc

Composer 安装命令:

composer require itlessons/php-ioc

包简介

PHP Inversion of Control(IoC) Library

README 文档

README

Very simple tool for managing class dependencies. Dependency injection is a method of removing hard-coded class dependencies. Instead, the dependencies are injected at run-time, allowing for greater flexibility as dependency implementations may be swapped easily.

Here is a simple example that shows how to register services and parameters:

use IoC\Container;

$c = new Container();

$c->setParameter('cache', array(
    'host' => '127.0.0.1',
    'port' => 11211,
));

$c->singleton('cache', function (Container $c) {
    $cache = new Memcached();
    $cache->addServer(
        $c->getParameter('cache.host'),
        $c->getParameter('cache.port')
    );
    return $cache;
});

// single instance of Memcached
$value = $c->make('cache');

Resolving a class dependencies:

use IoC\Container;

$c = new Container();
$c->singleton('cache', 'MemcachedCache');

class MemcachedCache extends Cache
{
    public function __construct(Memcached $cache)
    {
        $this->cache = $cache;
    }
}

$cache = $c->make('cache');

Resolving a simple class:

use IoC\Container;

$c = new Container();

class FooBar
{
    public function __construct(Container $c, Baz $baz)
    {
        $this->container = $c;
        $this->baz = $baz;
    }
}

$fooBar = $c->make('FooBar');

Binding an interface to an implementation:

use IoC\Container;

$c = new Container();
$c->bind('UserRepositoryInterface', 'DbUserRepository');

class UserController extends BaseController {

    public function __construct(UserRepositoryInterface $users)
    {
        $this->users = $users;
    }

}

$c->make('UserController');

Installation

The recommended way to install php-ioc is through Composer. Just create a composer.json file and run the php composer.phar install command to install it:

{
    "require": {
        "itlessons/php-ioc": "*"
    }
}

Alternatively, you can download the php-ioc.zip file and extract it.

Resources

You can run the unit tests with the following command:

$ cd path/to/php-ioc/
$ composer.phar install
$ phpunit

Links

[Принцип Inversion of Control (IoC) в вашем php проекте] (http://www.itlessons.info/php/inversion-of-control/)

统计信息

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

GitHub 信息

  • Stars: 13
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-02-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固