Tuesday 30 October 2012

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.





2 comments:

  1. Small bits of content which are explained in details, helps me understand the topic, thank you!

    Php classes

    ReplyDelete
  2. You are very welcome! I'm glad you found it helpfull

    ReplyDelete