Jump to content

Can someone please help me to understand why OOP is better than procedural?


ordinaryToucan

Recommended Posts

I apologize for yet another OOP-newby question!

 

I've read numerous tutorials and explanations of OOP, and I understand that it seems to be a good thing because it creates code thats extensible, reusable, and many other programming languages use OOP. So as an experiment I created a mySQL database class (which includes methods for connecting and various methods of constructing queries) to replace the library of (procedural) database functions I was using already.

 

However I can't see any particular advantage... and I hope someone can help me to understand why it will benefit me in the long run.

 

For example: before, I had a db_query() function that connected to the mySQL server (using login variables from a config.php file), and if you threw it a mySQL query it also selected the db, performed the query, and returned the result. I could call this function from anywhere.

 

Now, I have created a database class which includes a connect() method, a db_select() method, and a query() method which chains the first two together and then performs the query. Every time I want to use these I have to create a new database object...

 

This all seems like more work and I can quite understand what I am missing...?

Link to comment
Share on other sites

Here's 1 advantage:

 

I'll reuse your example. Say you have a function called DoesUserExist($username) that takes 1 param and it's a username. This function is in test1.php along with whatever stuff you want. Now say you want to use that function in test2.php. You would then have to re-write that exact same function because including it would include in all the echo strings that you put (if any). If you don't have any, and just have the function itself, then you can. You can make an entire PHP file with just functions though and include it.

 

Not the best wording. My point is that in a small PHP script, OOP may not be much, but in a large project, it's huge. It's also more organized, easier to read and understand (if you understand OOP better) and easier to edit in specs. I agree that at first (on a small PHP script) OOP is rather wordy.

Link to comment
Share on other sites

I think the key is encapsulation, and single point of use.

i.e. everything inside the class is a black box (no-one outside the class has any knowledge of it) apart from the public variables and functions, and becuase it's a single point of usage, you only have one place to correct your code, not multiple...

Link to comment
Share on other sites

I like it because you can reuse the code very easily, AND it's extremely easy to pass variables between functions.

 

For example:

class myExample {
    var $num = 1;
    function add() {
        $this->num++;
    }
    function returnNum() {
        return $this->num;
    }
}
$example = new myExample();
$example->add();
echo $returnNum(); //Returns 2
$example->add();
$example->add();
$example->add();
echo $returnNum(); //Returns 5

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.