carlosocarvalho / coci
php 简单注入
1.0.0
2015-05-27 22:18 UTC
Requires
- php: >= 5.4
This package is auto-updated.
Last update: 2024-08-29 04:14:28 UTC
README
PHP 简单注入,在项目中使用类。
## 安装 Composer install
{
"require":"carlosocarvalho/coci": "1.0.0"
}
## 示例使用
<?php
require "vendor/autoload.php";
use COC\COCI\Injection;
$inj = new Injection;
使用聚合创建类注入(Person)
Class Person(){
private $person ;
class function __construct(TypePerson $typePerson){
$this->person = $typePerson;
}
}
创建类注入(TypePerson)
<?php
Class TypePerson(){
private $type;
setType($type){
$this->type;
}
getType(){
return $this->type;
}
}
通过闭包注入类
<?php
$inj->set('TypePerson',function(){
return new TypePerson();
})
$inj->set('Person',function() use ($inj){
return new Person($inj->get('TypePerson'));
});
通过反射注入类
<?php
$inj->get('TypePerson');