<?php
namespace Database;
use ReflectionMethod;
class Database
{
protected $adapter;
public function __construct ()
{}
public function test (MysqlAdapter $adapter)
{
$adapter->test();
}
}
class MysqlAdapter
{
public function test ()
{
echo "i am MysqlAdapter test";
}
}
class app
{
public static function run ($instance, $method)
{
if (! method_exists($instance, $method))
return null;
$reflector = new ReflectionMethod($instance, $method);
$parameters = [];
foreach ($reflector->getParameters() as $key => $parameter)
{
$class = $parameter->getClass();
if ($class)
{
$parameters[] = new $class->name();
}
}
call_user_func_array([
$instance,
$method
], $parameters);
}
}
app::run(new Database(), 'test');
一个小Demo,帮助你理解ioc容器是怎么进行依赖注入的
最新推荐文章于 2019-10-20 18:54:47 发布