An introduction to Object Orientated Programming in PHP (OOP)

What is OOP?

OOP is a method in programming that allows you to encapsulate code into objects. Think of OOP as a bit like the human body. The human body has a lot of moving parts and organs that perform specific tasks such as heart, lungs, kidneys, liver and so on. Each of these has unique properties and functions which the brain calls on to carry out tasks when specific actions occur.

It is important to note that OOP is not a language but a way of writing code – to use another analogy when writing reports you may choose to write them in the first or third person. Niether is incorrect it’s using a different technique – for example writing in the third person is considered more objective – there we are that object thing again!

So how does OOP work?

OOP works by defining classes and then calling on those classes to either return variables or carry out functions or methods called ‘Objects’. By the way methods are the same as functions so if you hear the word Method in OOP they mean function.

Classes are like plans

Classes in effect describe what you want an object to be and what you want it to do. Let’s use a biology metaphor take DNA. Human DNA is responsible for describing everything from what the brain does, heart etc, how many legs you have, fingers even down to the colour of your eyes.

Each component described is a separate class. Through the biological process those classes are used to create the objects – heart, lungs etc. Now, some of those classes that the DNA has described have similar functions. For example the colour of your eyes may be similar to creating the colour of your skin so instead of writing the same function/method – in effect duplicating code why not share that function with other classes? We can do that in OOP land by using what is known as ‘inheritance’.

Visibility – Public, Private and Protected

Another key feature n OOP is visibility. What do I mean by that? Well, let’s look at the human body again. Take for example the pancreas – it is a very sophisticated part of the human body which has a method for controlling the flow of insulin – now the methods for making up the insulin are specific to the pancreas class – no where else in the human body is that created so therefore it makes sense to keep the variables and methods associated with it as PRIVATE. This keeps the code clean and modularised and prevents anything or erm… anyone (getting into science fiction now!) from accidently calling that function to do something in for example the heart – which could kill you!

Conversely we may want a method of a class to be used in more places than one. For example the heart beats as does your pulse – which occurs in your neck and wrists (it was last time I looked!) so we need the method responsible for controlling the rhythm to be public so we can call it where ever we need it rather than just the heart (that’s in your chest by the way.).

Another interesting concept in OOP is the protected status of either a method or variable. So what does this do? Well, here you have the best of both worlds – you can either access them publicly or they can remain private. To call them you would need to use a specific technique – usually “::” after the object that is calling it and before the variable its self.

So why not use just Public or Private?

Good question – the great thing about PROTECTED is that it almost acts as a fail safe. It is harder to call a protected function by mistake not least because a little bit of extra effort is required to call it. You can’t just type the name of the method or variable, attach it to the object and hope for the best!

A useful tip of when to use what and when…

What is private should stay private – anything else one could argue should be protected unless you know for sure it will be used regularly elsewhere – if in doubt go for protected. Some developers using nothing but private and protected, hardly using public at all. As you get more into OOP you will develop your own standard.

One last thing…

The above is not an exhaustive commentary of what OOP can do – let’s use some practical examples and along the way we will throw in some more concepts of OOP into the mix.

  1. Building classes
  2. Connecting to a Database in OOP and Procedural
  3. PDO Data Binding
  4. Scope – Public, Private and Protected