Using classes within other classes

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Using classes within other classes

Post by allspiritseve »

Sorry, must have misunderstood you.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Using classes within other classes

Post by josh »

My response was aimed more at the singleton advocates :P
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Using classes within other classes

Post by Christopher »

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
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.
(#10850)
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Using classes within other classes

Post by allspiritseve »

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.
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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Using classes within other classes

Post by Benjamin »

Maybe I can interject a different perspective here.

Perhaps there are two reasons for singletons:

1. Because the application demands that only once instance be created for logic purposes.
2. To save resources.

So that being said, with reason 1, it's an open and shut case for the most part. How you get an instance of the class is your preference.

With reason 2, there are other things to consider and things can get a bit tricky. This is something that only experienced developers are able to really do without ending up with bugs of some sort. I use reason 2 in my framework because things get incredibly fast. Again knowing when this can or can't be used boils down to experience or knowledge of the codebase.

I think I'm qualified to say this given that I can increase the speed of osCommerce by over 3000% with a little less than 5 hours of development time by making codebase changes.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Using classes within other classes

Post by josh »

arborint wrote: Those seem to be three pretty different things.
They are, that's what I was trying to convey.
astions wrote:1. Because the application demands that only once instance be created for logic purposes.
Not buying it. Then the code is not re-usable.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Using classes within other classes

Post by Benjamin »

jshpro2 wrote:
astions wrote:1. Because the application demands that only once instance be created for logic purposes.
Not buying it. Then the code is not re-usable.
I'm referring to the class, not the application. Sorry for the misunderstanding.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Using classes within other classes

Post by Christopher »

jshpro2 wrote:
astions wrote:1. Because the application demands that only once instance be created for logic purposes.
Not buying it. Then the code is not re-usable.
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.
(#10850)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Using classes within other classes

Post by josh »

astions wrote:I'm referring to the class, not the application. Sorry for the misunderstanding.
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.
Post Reply