I didn't find similar questions/solutions Googleing, or on Stackoverflow, so here I am, in need for help. I'm porting a PHP project from a custom framework to the Symfony framework (v2.5) which had it's custom dependency injection (based on class reflection). Many of the business logic classes have injected properties and I'm interested if there's a way to do something similar in Symfony without declaring those classes as services, because some of the classes are used for temporary holding/manipulating content coming from 3rd party endpoints and I need different instances of them.
More precisely is there a way to use Dependency Injection in combination with instantiating objects using the new keyword. So if I needed a new class that's dependent on logger or a validator, how could I do something like this.
Content class that I want to solve dependency injection for:
class Content extends SomeObject
{
private $pictureUrl;
...
public __construct(array $array = null)
{
parent::__construct($array);
}
public setPicture(...\Picture $picture)
{
// what's the best way to inject/enable this into my class
$errors = $this->get('validator')->validate($picture->getUrl());
...
$this->pictureUrl = $picture->getUrl();
}
...
}
The class that instantiates the previous class (in this particular case this could be a service, but there are other cases where this is another business logic related class):
class EndpointService extends Service
{
...
public fetchContent()
{
$dataArray = $this->endpoint->fetch();
$contentArray = array();
foreach ($dataArray as $key => $value) {
$content = new Content(array());
$content->setPicture($value->picture);
...
$contentArray[$key] = $content;
}
}
...
}
Note: If there are logical/syntactical errors please ignore them, this is an example through which I demonstrate my problem, and is a distant abstraction from my code base.