0
我想问一下设计模式。网关和存储库模式PHP Laravel
为什么我应该在构造函数中使用依赖注入,而不是导入它('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();
}
}
非常感谢。