Re: Using classes within other classes
Posted: Fri Dec 12, 2008 9:14 pm
Sorry, must have misunderstood you.
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Ok ... I continue to be bewildered. It is my understanding that Singletons are to provide one and only on instance of an object. It is my understanding that Registries are to provide a place to find common objects. And it is my understanding that Dependency Injection is where external code know how to instantiate and set objects that an object needs internally. Those seem to be three pretty different things.jshpro2 wrote:Look, i'm arguing singletons.. not registries.. granted 99% of people probably associate registries with singletons. The most common argument for singletons is "without a singleton, every object needs to have a reference to the commonly used objects".. simple un-true. With dependency injection the code expresses it's meaning, and will not execute without dependencies supplied. With a registry object you have to write code inside your class that make sure the dependencies are present in the registry. None of this has anything to do whether or not to use singletons though
I can see the similarities. If we're speaking in terms of finding common objects, a Singleton would have global scope, and no impact on the interface; a registry would have limited global scope, depending on how many classes used it, with minimal impact on the interface; a DI container would have very limited scope, and would impact the interface the most, but abstracts object construction so you don't need to worry about fulfilling dependencies.arborint wrote:Ok ... I continue to be bewildered. It is my understanding that Singletons are to provide one and only on instance of an object. It is my understanding that Registries are to provide a place to find common objects. And it is my understanding that Dependency Injection is where external code know how to instantiate and set objects that an object needs internally. Those seem to be three pretty different things.
They are, that's what I was trying to convey.arborint wrote: Those seem to be three pretty different things.
Not buying it. Then the code is not re-usable.astions wrote:1. Because the application demands that only once instance be created for logic purposes.
I'm referring to the class, not the application. Sorry for the misunderstanding.jshpro2 wrote:Not buying it. Then the code is not re-usable.astions wrote:1. Because the application demands that only once instance be created for logic purposes.
You know, most of the time you only want one instance and the way you achieve that is by simply creating only one instance. Singleton is for those time when you, as usual, only want one instance and you are actually having a problem of getting multiple instances. Saying that you only want one instance is not saying that you want a Singleton. Conversely there are situations where the problem occurs that Singleton solves.jshpro2 wrote:Not buying it. Then the code is not re-usable.astions wrote:1. Because the application demands that only once instance be created for logic purposes.
Indeed, OO programmers think of objects as their own "mini applications" that work together to solve big problems. Since you can create multiple objects that each have different state, the code is re-usable. Think of your application as a "singleton" your code and suddenly your code becomes not so re-usable.astions wrote:I'm referring to the class, not the application. Sorry for the misunderstanding.