Discussion in 'OT Technology' started by o2, Apr 10, 2007.
Can anyone tell me why/where I should use OO? Any websites that compare the 2 approaches?
when the scope of the project becomes large and somewhat complicated, the initial outlay for good oo design is offset by the flexibilty and ease of maintenance.
good oo design is not something you will pick up overnight. And once you do learn oo design, you will still need to learn at least a little uml and some basic design patterns before any of it will really pay off.
Use OO basically everytime. The only time I would not use OO would be a quick-n-dirty one-time throw-away linear script.
Any good readings on this subject? Perhaps some good online comparison of the 2 methods?
there's nothing to compare. If you are doing anything other than a quick-dirty, throw-away, never maintain script... Then you should use OOD.
Ive been using procedural method to maintain and add new features to my script almost weekly, and I dont find it a hassle or a problem. I just wanna know how OO will make things better....
think of a task that you do a LOT. For example access a database. Or display a header/footer on your pages. Or a function that you use a lot. Now OOD allows you to write-once, and use many times.
It also allows you to write your products to use generic functions that may run different back end code.
For example, I have a script that must access a database. I need to run a query, and return a recordset. With OOD, I can create generic methods and then substitute different classes depending on the type of database: MSSQL, MySQL, Postgres, DB2, flat-file, etc. Without changing a single line in my code.
By using it for presentation, such as header/footer/framework, you seperate the display from the function of the website. You also provide an "update once" "reflect everywhere" design. So I can update/change my framework in one class, and all pages loaded after that will use the new framework.
I thought about this for a while and I can't really think of anything except for ease of use when working with multiple coders. Is it any easier to maintain? Not really. I mean, to make a call to any other PHP file, whether it contains functions or an object you still need to include the source file.. when using an object you even have to take the additional step of creating a new object.
For personal/in-house use I'd stick with procedures.
If I were to share or publish code I'd probably go through it and OO it up.
Any time you're working with "things", like images for example, your project probably lends itself to OO design.
OO is good because it's easy to extend without breaking backwards compatibility, for example. You need to add new functionality to your Image class? No problem. All the old stuff still only uses the functions they were written to use. The new stuff won't break it.
It's good for reusing code. You're writing another program that has to handle images? Just include Image.php and you don't have to reinvent the wheel.
It's good for a whole lot of reasons. But if you don't want to be convinced, you don't need to. You can still use procedural design if you want.
What is the benefit of OO over putting everything into functions?
Everything that I use more than once I put in functions.php, and simply call that function where I need it. A function for connection to db, a function for displaying the date (x hours ago, exact date, x days ago, etc), a function for getting the username from an id, etc.
The main thing for me is re-usability.
I wrote a Form Handler class a few years ago and have been using it ever since. Speaking of which, I should probably update it. It has automated error handling, verification, and has some nifty arguments to parse regular expressions through strings and characters. The point of it is, it never needs to be modified. I can copy the class over to any project I'm using, create the class object, and call the objects. It's so simple.
I've yet to write any <form> related tags.
To me, projects tend to be a lot "cleaner" when developing/designing from an object oriented standpoint. When you know how to write OO code effectively, you really won't ever need to modify the classes unless you're doing a really depth change.
It's also more organized.