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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
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.

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)
      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;

            return "Error updating user: " . mysql_error();
      }#End update()
      function delete(){
            $sql = "UPDATE users SET status = 0 WHERE userId =" . $this->userId;            
            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)
            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;
      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

good luck

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
A couple of really good books - easy, quick reads - that summarize Object Oriented Programming:

The Object-Oriented Thought Process


Object Technology:  A Manager's Guide


The Object-Oriented Thought Process has good Java examples in it to really bring out the concepts.
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).
savache27Author Commented:
Thanks for the help everyone. I really appreciate it. I knew I could count on the place for some detailed answers.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.