Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the benefits of OO programming? Will it help me write better code? [closed]

Tags:

oop

php

I'm a PHPer, and am not writing object-oriented code.

What are the advantages of OO over procedural code, and where can I learn how to apply these ideas to PHP?

like image 835
Rich Bradshaw Avatar asked Sep 25 '08 19:09

Rich Bradshaw


4 Answers

It doesn't help you automatically. You can write worse "OO" programs than structural programs, and vice versa. OOP is a tool which allows you to create more powerful abstractions.

  • As with every powerful tool, you have to use it properly.
  • As with every powerful tool, it takes time to learn how to use it properly.
  • As with every powerful tool you will make mistakes.
  • As with every powerful tool you will have to practice a lot.
  • As with every powerful tool you should read a lot about it, and read what other people think. Learn from others.
  • But, as with every powerful tool, there are people out there who misuse it. Learn to not learn bad practices from them. This is hard.
like image 177
Paweł Hajdan Avatar answered Sep 24 '22 01:09

Paweł Hajdan


Objects help keep your code isolated between different sections, so that if you need to make a change to one section you can be confident it won't affect other sections: loose coupling.

Then, when you've done that for a while, you'll start finding that objects you created for one app are also useful in others, and you start getting better code re-use as well. So the new app has part of the work already done, and is using time-tested code: software is built faster with fewer bugs.

like image 22
Joel Coehoorn Avatar answered Sep 21 '22 01:09

Joel Coehoorn


People will tell you various things about OOP, from various perspectives. But if you want to form your own opinion, rather than take someone else's, then I suggest reading Bertrand Meyer's "Object-Oriented Software Construction".

Essentially, he takes non-OOP programming techniques, and analyses their basic flaws. He then derives an alternative technique which addresses those flaws. Put another way, he derives OOP from first principles. It's a marvellous piece of work, and very convinving.

Read it, you'll learn the why, when and what in a way that you can back up with reasoning.

like image 20
skaffman Avatar answered Sep 23 '22 01:09

skaffman


Objects help encapsulate complexity. For most PHP programming, it's impossible to write good, clean code for any reasonably complicated application. Writing OO PHP helps you put that code into its own box, isolating it from everything else. This has several benefits.

  1. As long as your object has clearly defined inputs and outputs, the way that the object does what it does doesn't matter at all - storing/retrieving data could go from flat file to XML to memcache to MySQL to Oracle, and you only ever have to concern yourself with one single object.
  2. As long as your object has clearly defined inputs and outputs, you can completely replace it with another object that has the same inputs/outputs. Decide at runtime whether you want MySQL, Postgres, memcached, or HTTP POST/GET requests to a sketchy server in Indonesia.
  3. OO makes unit testing easier. If you can define what a specific object should do (i.e what results it should give you for a given input) then you can easily write code to test thousands of values against that code and check the results, and you'll know instantly if something breaks.
  4. The more of your code you 'hide' in objects, the less of it you have to see when you're using that functionality. I wrote a polling application once in PHP that handled all aspects of polling - database interaction, poll generation, voting, ranking, sorting, and displaying - and I only needed one line of code on my website (Poll::Display()) to implement the entirety of what the app could do - which made maintaining my homepage far easier.

Keep one thing in mind - OO in PHP (even PHP5) isn't very good OO compared to a language like Python or Ruby. The everything-is-an-object model in Python is what made me OO programming really click for me - and as a former PHP programmer (and doubly-certified Zend engineer), I strongly recommend exploring Python's OO if you want to understand what OO is all about. It will help you write better PHP code, at the very least.

like image 23
Dan Udey Avatar answered Sep 21 '22 01:09

Dan Udey