• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 407
  • Last Modified:

OO programming VS. structured programming

Hi, I was wondering if someone could give me a brief explanation on the differences between object oriented programming and structured programming? Been searching the net but, I figured this is the best place to get a good explanation.
0
savache27
Asked:
savache27
1 Solution
 
_iskywalker_Commented:
what is structured programming? you mean procedural programming?
Object oriented programming is as it sounds all about programming with object. you make an object and you use functions on the objects and you use functions of objects.
procedural programming is, you have variables and functions, and on the variables  you use the functions.
Object Programming has surely more parts like inheration, i. e. an object inherate the properties of the father object.
with procedural you can do it, you can make structures of structures, but strucutres cant use in a secure and controlled way the functions of the other structures.
0
 
jmar_clickCommented:
Hi savache27,

Noticing the difference between OO and structured/procedural programming can be hard to grasp at first, even harder to appreciate the advantages of OOP. So i'll try to keep it simple. Best way to find out is to start building some classes and you'll understand.

Think of an object/class as a specialized group of functions with the sole task of handling all the needs of something, like a user.

Here is one of my user objects. Notice that the insertd and updates don't have data validation yet. Now I have 2 options, one, validate the data before I call the create() function, or better yet, validate the data within the create function, that way I minimize the code in my main users.php page and leave all processing to the class, thus separating the presentation layer from the data layer.



<?php
class user{

      var $userId;
      var $userName;
      var $passWord;
      var $status;
      var $createDate;
      var $userType;
      var $appId; #We'll use this when attaching a user to an application
      var $sortField;
      var $sortDir;

      function create(){
      //First - Create new user            
      $sql="INSERT INTO users (userName, passWord, userType)
      VALUES(
      '$this->userName',
      '$this->passWord',
      $this->userType
      )";
      mysql_query($sql) or die("Can't insert new user: " . mysql_error());
      
      //Second - Return new user id
      return $this->getLastRecord();
      
      }#End create()
      
      function read(){
            $sql = "SELECT users.*, user_types.userType FROM users
            INNER JOIN `user_types` ON (`users`.userType = `user_types`.userTypeId)
            WHERE status = 1 ORDER BY $this->sortField $this->sortDir";
            $rs = executeQuery(dopaging($sql,15));
            
            return $rs;
      }#End read()
      
      function update(){
      
            $sql = "UPDATE users set
            userName = '$this->userName',
            passWord = '$this->passWord',
            userType = '$this->userType'            
            WHERE userId = " . $this->userId;

            if(!mysql_query($sql)){
            return "Error updating user: " . mysql_error();
            }
      
      }#End update()
      
      function delete(){
            $sql = "UPDATE users SET status = 0 WHERE userId =" . $this->userId;            
            if(!mysql_query($sql)){
            return "Error suspending user: " . mysql_error();
            }
      }#End delete
      
      function detail(){            
            $sql = "SELECT * FROM users WHERE userId = " . $this->userId;            
            $sqlExe = mysql_query($sql);
            $sqlRs = mysql_fetch_object($sqlExe);
            
            return $sqlRs;
      }#End detail()
            
      function getLastRecord(){
            $sql = "SELECT MAX(userId) as lastId FROM users";
            $sqlExe = mysql_query($sql);
            $maxId = mysql_fetch_object($sqlExe);
            return $maxId->lastId;
      }#End getLastRecord()
      
      function addUser2App(){
            $SQL = "INSERT INTO applicationusers (applicationId, userId)
            VALUES(
            $this->appId,
            $this->userId
            )";
            mysql_query($SQL) or die("There was an error adding this user to the application list: " . mysql_error());
      }#End addUser2App
      

}#End class

?>

Take for example a contact form. You would either have all your sql updates, delete, select and insert statements on the same contact.php page. That can lead to bad spaghetti code. Then if you needed to read the list of users somewhere else you'd have to copy and paste the SELECT code again, unless you have it in a function.

The benefit of the user class/object is that when I create a new instance of the user class ($newUser = &New user;) I can assign values to the variables inside the object, and then retrieve them at any given time during the life span of the script. This makes updating, inserting, delete and updting very easy and very clean.

If I wanted to update a users record all i'd have to do is say: (and also remember all my data validation could take place inside the class)

      $userId = $_POST["userId"];
      $newUser->userId = $userId;
      $newUser->userName = $userName;
      $newUser->passWord = $passWord;
      $newUser->userType = $userType;
      $newUser->Update();
      
      header("location: manage_users.php?Action=Edit&Id=$userId&msg=Updated"); exit;

That beats doing the validation in the main page and having SQL statements here too.

I also have  pagination and a column sorting objects which handle sorting and pagination on tables. I never have to manually create pagination for reports, and users love to always have the ebility to sort by clicking column headers. And I love being able to implement those features by just coping and pasting 3 or 4 lines of code.

Here's some good reading to close things off
http://www.cs.uleth.ca/~cheng/courses/cs2620/slides/oop-4up.pdf

good luck
0
 
lisfolksCommented:
A couple of really good books - easy, quick reads - that summarize Object Oriented Programming:

The Object-Oriented Thought Process

  http://www.amazon.com/gp/product/0672318539/002-1897267-4657629?v=glance&n=283155


Object Technology:  A Manager's Guide

  http://www.bookpool.com/sm/0201309947

The Object-Oriented Thought Process has good Java examples in it to really bring out the concepts.
0
 
imladrisCommented:
Yeah, to be perfectly clear, the "opposite" of object oriented programming is procedural programming

You should be writing structured code in either case (though modern languages make it hard, or at least inconvenient, to do anything else these days).
0
 
savache27Author Commented:
Thanks for the help everyone. I really appreciate it. I knew I could count on the place for some detailed answers.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now