<?php
class Container{
// 存放容器的数据
public $instances = [];
// 单例模式
protected static $instance;
private function __construct(){}
public static function getInstance(){
if(is_null(static::$instance)){
static::$instance = new static();
}
return static::$instance;
}
public function set($key, $value){
$this->instances[$key] = $value;
}
public function get($key){
$new = $this->instances[$key];
return $new;
}
}
class Person{
private $count = 0;
/**
* Person 依赖 于 Car、Book类等等
* 所以 需要注入 Car或者Book类等对象才可以运行buy方法
*/
public function buy($obj){
$this->count += $obj->pay();
}
public function getCount(){
echo "这个人总共消费了{$this->count}元";
return $this->count;
}
}
class Car {
public function pay(){
echo "我支付了100000元买了这辆车";
return 100000;
}
}
Container::getInstance()->set("person", new Person());
Container::getInstance()->set("car", new Car());
$obj = Container::getInstance()->get('person');
$obj->buy(Container::getInstance()->get('car'));
更改后
<?php
class Container{
// 存放容器的数据
public $instances = [];
// 单例模式
protected static $instance;
private function __construct(){}
public static function getInstance(){
if(is_null(static::$instance)){
static::$instance = new static();
}
return static::$instance;
}
public function set($key, $value){
$this->instances[$key] = $value;
}
public function get($key){
$new = $this->instances[$key];
return $new;
}
}
class Person{
private $obj;
private $count = 0;
/**
* 更改的代码
*/
public function __construct($obj){
$this->obj = $obj;
}
/**
* Person 依赖 于 Car、Book类等等
* 所以 需要注入 Car或者Book类等对象才可以运行buy方法
*/
public function buy(){
$this->count += $this->obj->pay();
}
public function getCount(){
echo "这个人总共消费了{$this->count}元";
return $this->count;
}
}
class Car {
public function pay(){
echo "我支付了100000元买了这辆车";
return 100000;
}
}
Container::getInstance()->set("person", new Person(new Car()));
$obj = Container::getInstance()->get('person');
$obj->buy();
利用反射机制的容器
<?php
class Container{
// 存放容器的数据
public $instances = [];
// 单例模式
protected static $instance;
private function __construct(){}
public static function getInstance(){
if(is_null(static::$instance)){
static::$instance = new static();
}
return static::$instance;
}
public function set($key, $value){
$this->instances[$key] = $value;
}
/**
* 这个方法判断的依据 类的构造方法里面的参数
* @param $key
* @return object
* @throws ReflectionException
*/
public function get($key){
if(!empty($this->instances[$key])){
$key = $this->instances[$key];
}
// 反射类
$reflect = new ReflectionClass($key);
// 获取类的构造函数
$c = $reflect->getConstructor();
if(!$c){
// 如果没有构造函数,返回创建对象
return new $key;
}
// 获取函数里面的参数
$params =$c->getParameters();
if(empty($params)){
// 如果构造函数里面没有参数,返回创建对象
return new $key;
}
// 遍历构造函数里面的参数
foreach ($params as $param) {
$class = $param->getClass();
if(!$class){
}else{
// $this->get("Car") 事实上相当于返回new Car这个对象
$args[] = $this->get($class->name);
}
}
// 返回创建对象
return $reflect->newInstanceArgs($args);
}
}
class Person{
private $obj;
private $count = 0;
/**
* 更改的代码
* 注意这里Person类 依赖于Car 类
* 因此容器里面必须存在Person和Car这两个类
*/
public function __construct(Car $obj){
$this->obj = $obj;
}
/**
* Person 依赖 于 Car类
* 所以 需要注入 Car对象才可以运行buy方法
*/
public function buy(){
$this->count += $this->obj->pay();
}
public function getCount(){
echo "这个人总共消费了{$this->count}元";
return $this->count;
}
}
class Car {
public function pay(){
echo "我支付了100000元买了这辆车";
return 100000;
}
}
Container::getInstance()->set("person", "Person");
Container::getInstance()->set("car", "Car");
$obj = Container::getInstance()->get('person');
$obj->buy();