ordinaryToucan Posted January 6, 2008 Share Posted January 6, 2008 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...? Quote Link to comment https://forums.phpfreaks.com/topic/84765-can-someone-please-help-me-to-understand-why-oop-is-better-than-procedural/ Share on other sites More sharing options...
Highlander Posted January 7, 2008 Share Posted January 7, 2008 OOP does take more work, but when you getter better at it, you can reuse your code in another place in the project or maybe in another project. Reuse is the keyword when developing with OO paradigm. Quote Link to comment https://forums.phpfreaks.com/topic/84765-can-someone-please-help-me-to-understand-why-oop-is-better-than-procedural/#findComment-432536 Share on other sites More sharing options...
Ken2k7 Posted January 7, 2008 Share Posted January 7, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/84765-can-someone-please-help-me-to-understand-why-oop-is-better-than-procedural/#findComment-432586 Share on other sites More sharing options...
aschk Posted January 9, 2008 Share Posted January 9, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/84765-can-someone-please-help-me-to-understand-why-oop-is-better-than-procedural/#findComment-434395 Share on other sites More sharing options...
Pancake Posted January 14, 2008 Share Posted January 14, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/84765-can-someone-please-help-me-to-understand-why-oop-is-better-than-procedural/#findComment-438605 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.