Introducing Classes
April 17th, 2008 | by admin | |Just as functions are a way of grouping code that performs some specific purpose, classes are a way of grouping similar functions together. You can think of it as another step in keeping everything nicely organised
I find classes especially useful when working with databases in my web applications, so I’ll give you some examples along those lines.
What are classes?
In my world of organising data, I define the various ‘things’ in my system as objects. For example, an application may have ‘users’ and ‘products’. Consider these as two different objects, each of which require certain core functionality. Users may need to update their details, change their password, login. Products may just need their price updated, or other details. Each of these real world functions (update details, change password, update price etc) can be considered a core function of that object; something that needs to happen for this object to exist in your application. An object then, could be described as a combination of data and methods. And a way for us to define an object is by creating a class. The class is a template for an object. Following our examples, all we need to do is create a class, called ‘User’, and then create functions within this class for each of those real world functions. Then you have something that looks like this:
<?php
class User{
var $id;
var $name;
var $password;
var $details;
function change_password($newpassword){
#put code here to change user's password
}
function change_details($newdetails){
#put code here to change user's details
}
}
?>
Having written your class, to change a user’s details from within your application, you just need to call the class function, like so:
<?php
User::change_details('I am 26 years old and like chocolate.');
?>
Now that’s a very simple example, but you get the idea. It’s a good idea to get into the habit of using classes, because if your code ever does need to scale, or you become involved in a bigger project, classes will provide certain benefits, as we will further discuss.
An example data request when working with web databases:
- User asks for information from PHP page (Eg. Show me all products)
- PHP page asks PHP classes for data ( Eg. Get all products)
- PHP classes interact with database to securely get information from database (Eg. SELECT * FROM products)
- PHP classes give information back to PHP page
- PHP page returns information to user
- User marvels at the magic of the internet
Now we could, of course, just bypass the classes altogether and have the PHP page ask the database to get all information. So why use classes?
Why use classes?
Classes promote:
- Less time for development. Once our classes are written, we can re-use those same functions over and over, and classes also encourage code re-use across different applications you develop. Need another application with Products functionality? Take the classes/database from that previous project and whack them straight into your new project, done!
- Less code and less clutter in our application. Having classes takes the clutter out of our PHP pages, making them easier to read. This makes your life easier, as well as anyone else who may be reading/adding to your code in the future.
Classes solve the problem of scale with your code. The project can grow to massive proportions, but if you have your classes setup correctly, the transition is relatively painless compared to the alternative. Once you see a ‘classes’ folder in a web application, you know where all the logic is. You can then analyse the classes and get a feel for how an application works, and scale/modify it with ease.
Real world example
When your applications start to get larger in size, having a separation layer between interface logic and your database is a major bonus, as you can quickly find your way to all your SQL logic (you just have to look in your class files). This also means that if you decide to change to a different database engine, you can just make all the necessary modifications in your classes and the rest of the system will not be effected.
A great example of this happened to me very recently with this CMS. I wanted to know how many queries were actually happening behind the scenes in the creation of a page. Yes I had classes setup, but I’d taken it a step further (and this is something I recommend) and had a MySQL database class that all the OTHER classes talked to. Think of it as an interface to your actual database. Because I had classes that all talked to this main database class, all I had to do was add one global variable in my config called $NUM_QUERIES, then increment that variable in the SQL function that executes my SQL queries (one line change for my entire application in my MySQL class and config file!).
Suddenly, by the end of a page load I could output the $NUM_QUERIES variable and see that page1 had 66 queries. This was WAY too many queries, so I investigated and found I had a SQL query in some recursive logic (never a good idea), so I took SQL out of the loop and my number of queries went down to < 20 (which was more acceptable considering the security checks I have in place). So lessons learned, and very useful information for me as a systems administrator. Finding this performance problem only took about 10 minutes to implement across my entire application, all because I setup my system with classes ![]()