Say I have a large object - think EntityManager
of an ORM such as Doctrine
, or a custom DAO object, or what have you. Object, output of which is required to be used inside a class, but the object itself could be transient - do its job and then go out of scope.
It does not always matter if such object is large or not, but that it is not necessary to be a part of the class. However its byproduct is. Example:
//i.e. Doctrine\ORM\EntityManager
class BigTerribleDatabaseObject
{
//300 methods and 20 properties
}
When Not using DI
BigTerribleDatabaseObject
is only used to get some data and then it is discarded.
class Small
{
public getData($id)
{
$db = new BigTerribleDatabaseObject();
return $db->getDataById($id);
}
}
Using DI
BigTerribleDatabaseObject
becomes a permanent resident of the small class, despite being injected (it's injected and then it stays).
class Small
{
private $db;
function __construct(BigTerribleDatabaseObject $db)
{
$this->db = $db;
}
public getData($id)
{
$db = $this->db;
return $db->getDataById($id);
}
}
My Issue
I sometimes do a print_r
statement for debugging purposes on an object. Like so:
//first line depending on whether or not I use DI or not:
$myClass = new Small(new BigTerribleDatabaseObject())
$myClass = new Small()
print_r($myClass);
With DI $myClass
prints contents of Small
AND also all the content of the BigTerribleDatabaseObject
, because BigTerribleDatabaseObject
is part of Small
.
This floods my screen and sometimes even crashes my browser because in my case it is Doctrine's entity manager and it is huge and unwieldy.
Without DI I get a nice printout of just my very small class. Which I think is cleaner.
So I have to pick between "nice and small debug printout with no DI" or "use DI but suffer Large and unwieldy debug printout".
Question
I much prefer smaller printout. Does that mean I have to discard using Dependency Injection techniques? Can I use something else? Is that a necessary Evil of DI-ing large transient objects?