Moving from Procedural to Object-Oriented Development in PHP

PHP is one programming language that has welcomed self taught and relatively less experienced programmers with open arms. Alongside self- learnt programmers there is another group of programmers who have come from C background who have been programming the procedural way for years.

Now with increasing number of programs switching  to Object oriented programming in PHP, procedural programmers are feeling the need to update their skills.  In this article I try to take you through the process that hopefully will make it easier for you to move from Procedural to Object-Oriented Development in PHP.

What’s OOP ?

In contrast to procedural programming where we are driven by processes, OOP is driven by the underlying database structure.

Basic OOP Nomenclature:

  • A ‘class’ is the piece of code that acts as a template for creating one or more ‘objects’. A class in essence defines a new data type.
  • Functions inside classes are called methods
  • Variables inside classes are called attributes, properties or fields

With this understanding lets get our hands dirty writing our first OOP code.

For the first example, we define a class and create an object from the class

//this is how we create a class
class MyClass {
//We declare a variable - called attribute in OOP parlance
$a = "My first OOP Program"
//method within a class
    function foo() {
        echo $a; //first method of using variable
        echo this->a; //another method of using variable in OOP
    }
}

//obj1 is an object "of class A" - called instantiation of a class
$obj = new MyClass;

//here we access one of the object's methods
$obj->foo(); //accessing method within the obj will echo "My first OOP Program"

 

Introducing Class Constructor

Most of the OOP programs that you come across in real scenarios will have class constructors immediately after a class is declared.Constructors are included to ensure that your object is created with the right set of arguments passed on to it.

In PHP class constructors are declared using public function __construct().

//this is a class
class B {

    var $str;
    // this is the class constructor
    public function __construct($str) {
        echo $this->str ;
    }

    public function getStr() {
        return $this->str;
    }
}

//$a is an object "of class B"
$a = new B;

//here we access one of the object's methods
echo $a->getStr(); //outputs "Hello World"

In the example above, the presence of class constructor function __construct($str) indicates that to use this method correctly you will have to pass it one argument $str failing which the method in the class will not work.

Introducing Inheritance

Inheritance is one of the core benefits of OOP paradigm which allows for code reuse.

class Animals {
    public function __construct() {

    }
}

class Mammals extends Animals {
    public function __construct() {
        /*You can put code here*/

        parent::__construct();

        /*You can put code here*/
    }
}  

 

What’s with Code re-usability ?

So instead of say, littering mysql queries for inserting, updating or retrieving data all around the code, we define methods in the class database like

class MYSQLDB {
function query($query){
      return mysql_query($query, $this->connection);
   }

function addNewUser($fname, $username, $password, $email){
           $q = "INSERT INTO ".TBL_USERS." VALUES ('$fname', '$username', '$password', '$email')";
      return mysql_query($q, $this->connection);
   }
}

/* Create new database object connection */
$database = new MySQLDB;

/* No every time we need to add new users we can simply do it by calling on this object like*/
$database->addNewUser($fname, $username, $password, $email)

Final thoughts:
This post is intended to introduce the basic concepts of OOP as it applies to PHP. Learning to apply OOP in actual programs has a considerable learning curve which requires:

1) Knowing the syntax
2) Understanding the application in real world

This requires time. I was a procedural programmer for 5 years before I jumped to OOP and it took almost 6 months of regular effort to read about OOP and study programs designed in OOP.

But once you start getting the insights, its both a fun and rewarding journey.