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\EntityManagerclass BigTerribleDatabaseObject{    //300 methods and 20 properties}When Not using DIBigTerribleDatabaseObject 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 DIBigTerribleDatabaseObject 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 IssueI 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.QuestionI 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?
Do I have to stop using Dependency Injection to keep object debug printouts small?
object oriented;php;dependency injection
Note that in your current example, there is not much benefit from passing the object by the caller, since Small does not become decoupled from the database. So you should not stop using DI - quite the opposite, you should start to use it correctly.Instead of injecting the object of class BigTerribleDatabaseObject, you could inject a factory method for those kind of objects into Small to avoid the need for calling the constructor directly. Or better, the factory could directly encapsulate the database read operation to give your SmallFootprintInnocentClass a chance to be tested with a mock factory,  with no access to the real database. Something along the lines ofclass Small{    private $myDataFactory;    function __construct(function $myFactory)    {        $this->$myDataFactory = $myFactory;    }    public getData($id)    {        return $myDataFactory($id);    }}(forgive me if I got the syntax wrong, I am not a fluent PHP programmer).Since the factory will probably have no or minimal internal state, the print_r will result in a minimal printout.