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. 

PHP Classes - What is OOP?

What is OOP?


So, OOP. What is it and why is it so good? OOP, or Object Oriented Programming, is the term given to programing languages that either let you, or insist that you, program the code using general objects to create the processes that the program will perform.

Not too helpful huh?  OK, lets take a step back for a moment.

Some History

In the beginning (well not the beginning, as in 1843 Ada Lovelace beginning, more kind of late 1950's - 1960's type beginning) there was Assembly Code (there actually still is assembly code, it's just that these days it's something of a social recluse). Assembly Code is about as close as telling the computer exactly what to do as someone with higher language skills would ever want to get.  This language had no procedures, no functions (per say), no classes and no modules.  This meant that as the number of programmes working on any given project increased - so did the complexity of keeping the overall code intact.  As each programmer worked on a specific piece of code that had to interact flawlessly with every other piece of code.
As far as Assembly Code went that was manageable, because the language is low level, the commands that the language accept are pretty limited.  However, people got to thinking "there has to be a better way to do that", and (thankfully for the rest of us) they were indeed right.  Other Languages started being developed that were designed to act as a translator between the way the brain works and the assembly code used to make the program work.  And so the evolution of programming languages had truly begun (it was actually begun in the 1940's, during the second world war, by a very clever German gent called Konrad Zuse, but that's a different level of evolution).
So as things progressed languages got more and more "human friendly".  Part of this happening was alterations to how people programed.  The big step was the arrival of Procedural Programming.  This allowed programmers to write complex subroutines once within the program and call them for re-use as and when needed.  It also meant that a group of programmes could write independent procedures for the same program and  these procedures could be incorporated into the overall program in a much more modular way, allowing for much smoother integration of code into the overall program.  Procedural programming was great.  At the time. Now its been relegated to the standing of only "Damn Good" and title of Great has been taken by the Object Oriented kid from three doors down.

Now To The Point

Object Oriented programming took Procedural programming and pimped it up.  Procedures became known as Methods within the objects known as Classes.  Instead of having a whole list of procedures loaded at the start of the program now you can have a whole bunch of objects that are only loaded when they are used by the program.  These objects can also be stored in a library and are generally easier to organise than the Dewey Decimal System.

So What's This To Do With PHP Then?

Well PHP is an object oriented language.  However, it's not strictly an object oriented language.  What that means that one of the things that makes PHP so much fun to use and what makes PHP so easy to pick up, is that it is what's called a "loose language".  This means that it isn't strict about how you program in it, as long as PHP can make sense of the logical steps you have used to write the program then you will get away with it.  Other languages aren't quite so relaxed about how you code - Java by SUN/Oracle for example will have no hesitation in throwing the toys not only out the pram, but squarely at your face if you try and code without using objects.  Yes choice is good, but then again, so are rules.  PHP's loose nature can, and very often does, cut both ways.