Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Object oriented programming in PHP

Wednesday, September 8th, 2010 by Servage

If you have no idea what object oriented programming is, you should definitely read on! This article is for you ;-) Many newcomers to the world of web design and web programming get involved with programming scripts in PHP at some time or another. PHP is a very extensive language and enables you to create virtually any application you like (of course you can always discuss if another language might be more suitable for a specific purpose, but as a rule of thumb for beginners, learning PHP can be a great step forward). When programming with PHP, it doesn’t take long before you start using and creating own functions. That’s cool! But at some point many developers start “overdoing” it. Why? Mostly because of unawareness of more abstract programming techniques, that might unleash a whole new dimension of possibilities for your project. So take a few minutes and learn about object oriented programming. It will help you!

Object oriented programming is basically a concept of creating your code around a data-model. In plain language an object is a representation of the items your use in your application. For example if you have a website with a user/login area, you are having a “user” data model. It could be backed by a MySQL database that contains a users table, with an entry for each of your users.

The classic (and let me call it wrong) way of working with users via functions, would be something like this:

function createNewUser($username, $email, $birthday) {
  $sql = "INSERT INTO `users` (`username`,'email`,`birthday`) VALUES ('".$username."', '".$email."', '".$birthday."');
  $result = mysql_query($sql);
  if ($result)
    return true;
    return false;

In your PHP script where you create a new user you would simply call the function:

if (createNewUser('bobby','','1975-10-15'))
  echo 'User created';
  echo 'Error: User could not be created/';

This way of coding works fine until a certain point, when you maybe not just have users, but also other data models. Maybe it’s a blog, so you also have the models article, comment and rating. Working with objects makes it easier for you to work with your code, as it naturally becomes more easy to understand, and at the same times increases the possibilities to extend your project, reuse code in other projects and more.

So how do you start programming object oriented in PHP?

Well, it’s not that difficult. First of all, I suggest creating a separate .php file for each of your objects. For simplification, in PHP these are also called Classes. So say you have created user.php that you include wherever you need the user object. It would be something like this:

Class User {
  var $username;
  var $email;
  var $birthday;

  function create($username, $email, $birthday) {
    $this->username = $username;
    $this->email = $email;
    $this->birthday = $birthday;
    $sql = "INSERT INTO `users` (`username`,'email`,`birthday`) VALUES ('".$this->username."', '".$this->email."', '".$this->birthday."');
    $result = mysql_query($sql);
      if ($result)
        return true;
        return false;

  function sendMail($subject, $text) {
    // ... code to send an email to the user ...

Now, this might first look more complicated than the old function, but the more functions you have for your users, you will find how this streamlining and centralization of functions (called class methods) actually helps you! In your code, the creation of a new user would now be:

// Create new user
$user = New User();
$user->create('bobby', '', '1970-10-15');
// Send mail
$user->sendMail('Hi there!', 'I'm glad you were created in my system! Regards!');

So, what can you learn from this? Try to thing in “objects” when you develop. Creating and using a lot of “regular” functions that you create is generally something that you should avoid. Pretty much everything can be wrapped in a (data) model.

I suggest you read WikipediaPHP Class Basics and  PHP Abstract Class tutorial.

Object oriented programming in PHP, 4.6 out of 5 based on 13 ratings
Categories: Guides & Tutorials

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

1 comment (leave a comment)

Good example thanks, Object Oriented Programming very easy :)

Leave a comment

You must be logged in to post a comment.