Your classes should not depend on any objects that are not either
injected into the class or created in the class itself. If they do,
you cannot ever change that dependency from outside, which reduces the
flexibility and testability of the class.

If you use your class without having a context (f.i. in a task) you'll
get the infamous error message that this thread is titled with. The
problem is that this dependency is not visible from outside - there is
no $context parameter in the constructor, nor a method ->setContext()
or the like. Furthermore, no exception is thrown by the class itself
that a context is required, thus the error is hard to debug.

Regarding testability: In unit tests, you usually substitute most
dependencies (the objects that the tested class depends on) by fake
objects, that is, objects that act "as if" they were the real objects
without having a real implementation. The benefit is that the test
setup is much easier to do (imagine reading factories.yml before every
unit test just because you need the context!), it runs much faster and
it does not fail if the dependency is buggy (in that case, only the
test for the dependency itself should fail).

Now if your dependency is hardcoded in the class, you cannot ever
substitute it by a fake implementation in the test. Thus, always
inject all dependencies through the constructor (if they are required)
or through a setter (if they are optional).

If you want to learn more about dependency injection, I suggest to
read Fabien's very good blog posts:
http://fabien.potencier.org/article/11/what-is-dependency-injection


Bernhard

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"symfony users" group.
To post to this group, send email to symfony-users@googlegroups.com
To unsubscribe from this group, send email to 
symfony-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/symfony-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to