Apologies. It’s been literally months since I did my last tutorial here. Most of my tutorial effort has been focused on FOSSwire. Anyway, I’m back now and thanks to a request from Nick I’m starting a new multi-part tutorial today.
Now this has been covered in many places before and the subject is object-oriented programming (OOP) in PHP. For the purposes of this tutorial, I’m using PHP 5.1.6 on Linux, but all of this should work on PHP 5.x on any platform and most of it will work in PHP 4.x.
Before we jump in and start coding, what exactly is object-oriented programming? Well, Wikipedia defines it as:
Ouch. Let’s break it down.
Object-oriented programming, as the name suggest, revolves around objects. Now I could go into the whole theory behind it, but I’m going to try and jump in and get some key terms in your head rather than going through everything. This isn’t a long-winded introduction to the fundamentals behind OOP – this is a crash course to get you up and running with everything you need, but not a lot more.
A class is one of the very useful things we will be using in OOP. In the simplest sense, you can use classes just to group similar functions and subroutines together. For now, that’s all we’ll do.
OK, I have a few functions that deal with my database that my app often uses.
You’ll notice that these are all to do with the database. Now at the moment, it’s fairly easy to reuse these functions. We can store them in a separate file and just include() or require() it and call our functions.
But – what if we have loads of functions? What if we have functions’ names clashing? Now things start to get messy and you can quickly lose track of your functions, variables and pull your hair out. Oh – and if you want to reuse your code in another project, you have to go hunting through hundreds of lines of useless stuff to find what you’re looking for.
So, we’ll put all this database stuff in a class. There are also other benefits of using classes, which we’ll look into a bit later.
Warning – this is not a very good tutorial at explaining classes and the OOP ideas, as I’ve said. I’m getting you up and running with OOP in PHP, not explaining OOP. You should read up on OOP first ideally.
To declare a class, use the class keyword and then give your class a name. I’ll use dbIO as mine – for database input/output.
Converting to classes
OK, so let’s convert our set of functions into a DB input/output class. Here’s the full code.
Wow! So, how do we actually utilise this code elsewhere.
To use this, we need to create a new ‘instance’ of our class. If you’re not familiar with this term already, think of a class for a cat (a domestic one, if you please). With just the class, we have a definition of what a cat looks like, and we have some functions of things cats can do (it can meow).
But we don’t have a cat until we create an instance of this cat.
Likewise – we have the definition of all the stuff we can do with our DB, but we don’t have an ‘object’ we can use until we make a new instance (or instantiate it). We need to make a new variable and use the new keyword to do this.
Provided you’ve included or required the file where your class is (if it’s separate) then just do this:
Now, we have access to all the functions (and variables, but I’ll look at them later) in this class.
Let’s call checkEmailAddress() and pass it in an email address, shall we?
As you can see, we use the -> notation to say to PHP “go into the instance of the class pointed to by $dbobject, and find the checkEmailAddress() function”.
This doesn’t do anything useful yet, as at the moment we’re simply chucking away the returned result from our email address checking function, but now you’ve got this, you’re somewhat there. At least you can use the ‘organising functions’ purpose of classes, anyway.
In Part 2, I’ll be looking at variables inside our classes, passing data between functions inside the class and getting variables from outside.
So, until then, enjoy!