PHP: Procedural vs. Object Oriented Coding Style

PHP allows you to write code in two flavours, one is procedural and the other
is object oriented. You can even write procedural code in PHP5 and it will run
without any problems. If you are not clear about procedural and object oriented
programming, then we will have a look at these two different coding styles. The
following two examples are not fully running examples rather a pseudo code:

$user_input = $_POST[‘field‘];
$filtered_content = filter($user_input); //user input filtering
mysql_connect(“dbhost”,”dbuser”,”dbpassword”); //database
$sql = “some query”;
$result = mysql_query($sql);
while ($data = mysql_fetch_assoc())
process ($data);

You will notice using a lot of inline processing either directly or via using functions.
It may stand as an example of typical procedural operation. Let’s see how it looks
after converting it to OOP:

$input_filter = new filter();
$input_filter->filter_user_input(); //filter the user inputs
$db = new dal(“mysql”); //data access layer
$db->connect($dbconfig); //using mysql
$result = $db->execute($sql);
ReportGenerator::makereport($result); //process data
$model = new Postmodel($filter->get_filtered_content());

Now if you take a look into these two code snippets, you will find that the latter
one is much more readable. Well, you can make the first one more readable by
introducing some more functions into it, but how many functions are you ready
to search into when you use them? The latter snippet is better organized because
you know which object is handling which process. If you write big applications in
procedural style, it will be almost impossible to manage after a few versions. Of
course you can implement strict coding conventions, but it is agreed by millions
of developers that it won’t give you the ultimate manageability and usability if it’s
procedural unless you do it in OO style. Almost all big applications are written using
the object oriented approach.

Object-Oriented Programming with PHP5
Chapter 1, page 24

Published by Miaz Akemapa

Book minded!!!

Reader Comments

  1. Interesting. The post seems to be pretty biased towards the OO aproach (which is understandable, coming from a book about OO). The real answer is that there is no competition between the coding styles – procedural is best suited to certain programs, while OO is best suited to others. It cannot be said that one is simply better than the other.
    Procedural programming flows more naturally with the way PHP parses code, so code written procedurally tends to be faster and often smaller than OO code.
    However, as the post mentioned, procedural code can get very messy, and is not easy to document. Object oriented PHP is much better in this area, as it is infinitely more readable and understandable (if the reader knows OOP), which makes updating or adding new functionality to programs much easier.

Leave a Reply

Your email address will not be published. Required fields are marked *