1)阅读本文以了解有关laravel 5中方法注入的更多信息
原文链接:https://www.f2er.com/laravel/132538.htmlhttp://mattstauffer.co/blog/laravel-5.0-method-injection
https://laracasts.com/series/whats-new-in-laravel-5/episodes/2
2)这是方法注入的简单实现
$parameters = []; $reflector = new ReflectionFunction('myTestFunction'); foreach ($reflector->getParameters() as $key => $parameter) { $class = $parameter->getClass(); if ($class) { $parameters[$key] = App::make($class->name); } else { $parameters[$key] = null; } } call_user_func_array('myTestFunction',$parameters);
你也可以看看功能
public function call($callback,array $parameters = [],$defaultMethod = null)
在https://github.com/laravel/framework/blob/master/src/Illuminate/Container/Container.php
文件了解更多详情
App::call('\App\Http\Controllers\Api\myTestFunction');
或方法
App::call([$object,'myTestMethod']);