I am still trying to wrap my head around some of the aspects of the Dependency Injection design pattern using Pimple. I completely get the concept of using a constructor or setter function belonging to class Foo to establish it's dependency on class Bar.
The part I don't quite get is how to properly instantiate multiple new instances of class Bar from inside a method belonging to Foo while using a Pimple factory.
Basically I want to accomplish the equivalent of this:
Block.php
class Block {
private $filepath;
public function setFilePath($filepath) {
$this->filepath = $filepath;
}
}
Page.php
class Page {
private function newBlock() {
//do something here to get $filepath
$block = new Block();
$block->setFilePath($filepath);
return $block;
}
}
I am using Pimple for my container like so:
bootstrap.php
$container = new Container();
$container['page'] = $container->factory(function ($c) {
return new Page();
});
$container['block'] = $container->factory(function ($c) {
return new Block();
});
The idea is that there can be multiple pages defined, and each page is potentially comprised of multiple blocks. The properties for each block are defined by methods within Page. And it needs to happen using entirely decoupled code.
It is my understanding that injecting the entire container into Page as a dependency is actually the Service Locator anti-pattern. So the following is BAD code:
class Page {
private $container;
public function __construct(Container $container) {
$this->container = $container;
}
private function newBlock() {
//do something here to get $filepath
$block = $this->container['block'];
$block->setFilePath($filepath);
return $block;
}
}
How do I go about giving Page the ability to use the Block factory that is defined in the DIC?