Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Object Oriented or not? [closed]

I have a start of a webapp that I wrote without using the Object Oriented features of PHP.

I don't really know if it is worth it to go back and rewrite the parts I have finished. Is object oriented PHP worth rewriting all or part of a decent working app?

like image 619
Adam Lerman Avatar asked Sep 11 '08 20:09

Adam Lerman


2 Answers

Given that you have an incomplete app I would say that reworking it into an Object based app will probably be helpful.

One thing to consider is the expected size of the end application. Below a certain complexity Object based may be overkill except for the learning experience.

I started out avoiding Objects like the plague because my initial introduction to them in university classes was terrible. I somewhat recently had to work on a project which was implemented in php objects. making the required changes was much easier than other projects. I have since then worked in the object model frequently and find it very handy for quick creation and easier upkeep.

like image 172
Laith Avatar answered Nov 16 '22 05:11

Laith


Just to disagree with the consensus... I would say no in most cases. Not as an academic exercise on commercial code anyway. If it's working don't re-write it. If you have to go in to change / add bits, then refactor towards OO practices (there are lots of posts on SO about refactoring when you are changing code anyway, and not just for the sake of it).

In practise if you haven't done a lot of OOP, then you'll want to start small and get a feel for it.

Once you get a handle on the basics, a good beginners guide to Design Patterns (I like the Head First book) is very useful. Most PHP books would teach you OOP fairly poorly. They teach you about inheritance, but usually don't teach you about loose coupling and favouring composition over inheritance. A design patterns book will give you a better insight into this.

PHP still has a reputation for not "doing" OO right. I don't think this is fair, but is a reflection of the fact that it's so easy for people to get started without really grokking OOP. I would go out on a limb and say the majority (ever so slightly - call it 51%) of PHP programmers aren't comfortable with OOP. I think it's possible to do good OO in PHP, and if you're already comfortable with the language it's a great way to grow your skills.

EDIT:

Just to add a couple of disclaimers...

  1. My comment about most PHP programmers not being comfortable with OOP wouldn't apply to the current SO audience!
  2. Not suggesting you aren't comfortable with OOP, this applies if you're not
like image 12
reefnet_alex Avatar answered Nov 16 '22 04:11

reefnet_alex