class One
{
private $closure;
public function __construct(Closure $closure)
{
$this->closure = $closure;
}
public function doSomething()
{
if (...) {
// 用的时候再实例化
// 实现懒加载
$instance = $this->closure();
$instance->do(...)
}
...
}
}
...
$instance = new One(function () {
// 类One外部依赖了类Two
return new Two();
});
$instance->doSomething();
...