This is the first post in a short series where I will share with you a database handler class that I built to use in my web pages. It’s all done using PHP and PDO – my preferred method of interacting with a MySQL database.

I’m sure my database handler isn’t the best in the world ever, so I’d love to hear from you guys if you have any improvements or suggestions! But it has served me well in the past, and I’ve never really had time to revisit it – so let me know what you think. I hope it proves useful to you as a learning exercise, as well as a handy method for interacting with your MySQL databases.

I have implemented the DBH ( database handler ) as a class – some of you may know this is using the Object Orientated part of PHP. For this reason this first post will be mainly concerned with introducing some rudimentary points of OO ( Object Orientated ) programming. You will learn how to build a class, how to instantiate it as an object, what a constructor is, and how to use the constructor to set initial values of your object.

You will need a working MySQL database for this series of tutorials. If you don’t have one and don’t know how to build one, this post will take you right through from step one.

The Database Handler Class

Let’s put together the bones of our class. First the code, then the explanation:

class DB_Handler
{
public function __construct(){
}
}

In the first line we are declaring the class and giving it a name. In OO, a class can be most easily described as a collection of variables and functions related in one functionality and purpose – so in this case everything within this class is concerned with working with a database.

A few things to note:

In OO, a function is known as a method, and a variable is known as a property, so I’ll be using those terms from now on.

When we use a class, ie call it in our code, it becomes an object. An object is an instance of a class. We have only one representation of the class, but can have as many objects of that class as memory allows. If this seems confusing, don’t worry, it will become clearer as we continue. Think of it like having a snowboard mold in a factory ( the class ), which produces blank snowboards ( the object ). There is one class, but many objects, each of which can have different colours, designs etc

So, back to the code. We have our class declared and you can see that we have a method in there with a funny name – __construct().

This is the constructor of the class, and is a method that is called every time an object is instantiated ( when an object of the class is created ). In other words, any time you want to use the DB_Handler class, you will implicitly call the constructor. Great, but what’s the point of it? Hold your horses. Let’s keep going and all will become clear…

Properties

I have added a number of ‘private properties’ to the class. Simply put, these are properties that can only be accessed or changed by methods within the class. In this case, it is sensible to set the database host, name, username and password as private properties as these values will be unlikely to change during the lifetime of the object.

That is, we will instantiate an object to talk to our, for example, snowboard database. This object will then want to have set as it’s private properties the host of this database, the name and the auth details.

These properties will be particular to the snowboard database, and will not change ( we hope ) for the snowboard database. Sounds sensible – so how do we set these values in the first place?

Instantiating an Object

Sounds fancy doesn’t it? It’s the sort of term you can throw around with your non-programmer pals to show what a genius you are. And after this turorial, not only can you say it, but you’ll know what it means. So…

As we know, instantiation is just a fancy term for creating an object from a class. Think back to the idea of the class as a mold in a factory, and the object as the real life thing that we get from the mold. Instantiation is the process that makes the real life thing from the mold – the object from the class.

It sounds like a lot of work, but it’s not. It’s simple. Just one line of code:

$snowboard_db = new DB_Handler();

Using the Constructor

There, you’ve done it. We now have an object called ‘snowboard_db’. But we still haven’t set any of the properties we talked about before. It would probably be sensible to do this at the beginning of the process, whilst we instantiate the class, wouldn’t it?

Yes it would. And this is where the constructor we talked about earlier becomes handy. Let’s revist that code, but with some additions. First:

We have changed the constructor’s method so it now accepts a number or parameters, being in this case the values we talked about before. So now, when instantiating the object, we can pass these values to the new object like so:

When we instantiate a new object, anything we pass to the class will be passed to the constructor. So let’s now handle these new values we are passing to our new object. Back to our constructor method:

The constructor now accepts these values, and uses a special keyword called “$this” to assign the values to the new object’s properties. $this refers to the object that the constructor resides in, and is used throughout to refer to other methods and properties of the object. It does this by using the operator ‘->’ to link $this to the property or method being used.

In this case, $this->host, will refer to the private property of the object, $host. Not that the ‘$’ of the property name is replaced with the operator ‘->’. We can refer to the property $host anywhere throughout the object by using the ‘$this->’ construct, followed by the name of the property.

This may seem confusing at first, but it isn’t really, just kick back and keep using it. It will all become clear with practice. Just remember that when referring to a property or method of a class from within the class, use the ‘$this->’ construct.

Summary

I think that’s enough for one tutorial. A lot of new ground has been covered in this post, and it may take a few reads and working with the examples to let it all sink in. If things still aren’t clear, then please drop me a comment and I’ll get back to you.

We have put together the beginnings of a new class, we know how to instantiate a new object of this class, and we know how to use the constructor to set the initial private properties of the object. I think you deserve a cup of tea now.

In the next part we will be connecting to our database and learning how to handle errors. Until then, happy coding.

Great post. But in my opinion you ended this tutorial in a premature state. I apologize if that word is a bit harsh (english isn’t my first language). What I want to say is when the steps were beginning to become exciting to me, that’s when you stopped writing. Anyway I believe part 2 is already out. I will read it now. Thanks for giving me an idea that I can make a class to handle the database connection.