PHP: The Basics of an Object Oriented Framework

I do a lot of PHP development. It’s what I do for a living. Over the years I have more than realized the wrong way to develop web applications, and the right way. I am going to share with you today a few tips, and some basic code, on how to do it the right way.

First, let’s talk a little about what object oriented design actually means. When you build an application, and use classes, that doesn’t make it object oriented. As a matter of fact, more often than not, when reviewing other people’s code, classes are used in horrid ways that completely defeat the value of classes. Here is the basic idea of the three common, useful ways to use classes.

Model Class: A model class is generally a class that represents an object. An object can have data properties, or the object can just be a tool you use. Say for example, we are building a blog. The base of the blog would be a blog post. In a object oriented design, a blog post would be an object, aka, a class. So, we would make a simple class, that handles a few blog related tasks such as posting a blog, and updating the blog. A blog may seem a little mundane to encase the functionality into a class, but let’s say we have a user system. A user would also be an object, and store all the user based info in that object. Where this shines, is say we have a list of user’s, or say user comments, we would easily be able to pull user data using a simple function call, such as $user = new m_user($user_id). Now, you have access to all user data in the $user variable you just created, so accessing info is easy, and is consistent across the board. Not to mention, if the way data is stored changes, you only need to modify the model, and can leave everything else alone.

Controller Class: A controller class is the C in the MVC framework. A class that doesn’t store data, but acts as a way to organize similar functions, or act as a gateway. A controller’s job is to grab the object data that is needed for this function to be displayed, and then call whatever display function is there to display it (be that a templating engine, another PHP file, whatever).  Say I had a class named blog and inside it had a function called view. A simple MVC framework would make a call to that function based on the URI, something like The view function in the blog controller would then handle what happens from there. Setting the necessary objects, and passing the data on to the display method.

Helper Class: A helper class is a wide variety of classes. A helper class can be a database abstraction layer, or it can be a set of function for an API. These classes are useful for managing mundane tasks, and manipulating data that doesn’t rely on the database.

Those are the three basic uses for classes. The worst class use I have experienced is when programmers use a class, just to store a bunch of functions that may or may not be related, that have absolutely no benefit from being encapsulated in a class. The general rule of them is, if there is no organization, or data manipulation benefit, you’re doing it wrong.

I know I didn’t spend a whole lot of time breaking every aspect down, but this does give a very general idea of how logic based, object oriented frameworks, well, work. My next article will be a simple MVC tutorial, demonstrating how to build a basic MVC framework in PHP, and build a blog off that.