Tuesday 30 October 2012

PHP Classes - What is an Object?

So What Is An Object?

This isn't an easy a question to answer, and I am sure by the time I am done I will have you wishing you never asked, but you did so here it is, and as I have just renewed my membership to the "Analogies Fan Club" I'm going to make use of a rather drawn out analogy to help me with this.
For this analogy I am going to assume that you live in a house, know someone that lives in a house, or have at least had a walk about inside a house.  As I hope you will all agree, a house is an object (in the physical world at least).  Well lets take that and make it an object in the programming sense.  A house has one or more rooms.  These rooms, when transconceptionalized (yes, I just made that up) into the programming sense become methods.  So taking a general 2 bedroom house with lounge, kitchen and bathroom gives us an object called house with methods called lounge(), kitchen(), bathroom(), bedroom1(), and bedroom2().

Right, so that was easy enough.  Now to deal a little bit with "Scope". Scope is the term used to describe how visible a method or a variable is.  You can have variables within the root of the object as well as within each of the methods.  And just like rooms in a house variables of the same name that are in different rooms are kept apart from each other. so if you have a variable called TV in lounge() and a variable called TV in bedroom1() anyone who comes to the house to watch TV can be sent to either of these rooms either by you or by knowing when they arrive, if you made the TV variables public, which room is showing the show that they want to see.  So that's variables within the methods touched on, now for the variables in the root of the object.  These variables are generally used to describe the object, or to hold the information withn the object that isn't specific to the methods.  So a house could have variables that state the people who live there, the amount of electricity gas and water the house uses, phone line information, how many times there has been a delivery to the house by a courier and what the courier company was.  These do not need to include variables for things within methods that can or often do pass into other methods.  So for example you do not need to have an object level variable called "$dadsCoffeeCupLocation" to track the movements of said sacred artefact around the methods of the house. each method can relate directly to any other method within the object and pass information that way.  What's more, a method can also be an activity that goes on in the house, as well as the room in which it happens.  So you could have a tracking method that is dedicated to the task of keeping track of "$dadsCoffeeCup" as it is passed between each room method.

So, starting to sound more than slightly complicated?  Believe it or not, it's actually a good thing if it is.  Objects are simple on the outside and complicated on the inside.  This means that, once you have coded the complex stuff once at the start, it's really dead simple to access the stuff that the object contains. 

No comments:

Post a Comment