我需要帮助解决一些问题。这是Laravel 4.2,它来自一个博客,没有办法联系作者或发表评论。。
http://vegibit.com/what-is-the-ioc-container-in-laravel/
有人能给我一个例子,说明如何在他的例子中使用对象文字符号来访问依赖对象吗。
$myCar = App::make('Car');
因此,我想访问Bridgestone对象的胎面属性。
echo $myCar->Tire->Bridgestone()->tread;
这不起作用,但这说明了我正在尝试做什么。我正在尝试打印"性能"
谢谢。。。。
变量受到保护,因此尝试添加返回注入对象的公共函数:
class Car {
protected $tire;
protected $engine;
public function __construct(Tire $tire, Engine $engine) {
$this->tire = $tire;
$this->engine = $engine;
}
public function tire()
{
return $this->tire;
}
}
class Tire {
protected $bridgestone;
public function __construct(Bridgestone $bridgestone) {
$this->bridgestone = $bridgestone;
}
public function bridgestone()
{
return $this->bridgestone;
}
}
你对踏板的要求是:
echo $car->tire()->bridgestone()->tread;