【发布时间】:2016-03-10 03:25:42
【问题描述】:
我想问一下设计模式。
为什么要在构造函数中使用依赖注入,而不是导入它('use statement')?
例如:
在我的控制器中:
class AuthController extends Controller {
public function __construct(UserGateway $userGateway)
{
$this->userGateway = $userGateway;
}
public function doSomething()
{
$this->userGateway->foo();
}
}
为什么不直接使用呢?
use Acme\UserGateway;
class AuthController extends Controller {
public function doSomething()
{
UserGateway::foo();
}
}
非常感谢。
【问题讨论】:
标签: php design-patterns repository gateway