views:

57

answers:

2

So I am thinking of doing my PHP in OO way. Can someone tell me the advantage/ disadvantage of this? I think one advantage will be that my codes will be cleaner and easier to maintain. What about performance? How does doing PHP in OO way compare to just writing codes in non-OO way.

My idea of an OO style is, for example, when handling e-mail. I can just do mail(.....). But instead, I will create a class called EmailManager. It will work something like this

EmailManager em = new EmailManager();
em.addSender("[email protected]");

and so on...

A: 

You've basically hit the reason. Within PHP in particular, code can end up unstructured and unmaintainable. By creating language-level constructs (objects, in this case) that reflect the tasks that you wish your program to perform, you make your code more closely match its function, while separating concerns and making your life easier if you need to modify the code (or reuse parts of it!) later.

Gian
do you know whether doing it this way will worsen my program's performance?
denniss
It is unlikely to impact performance in any meaningful ways. As long as you don't try to do anything totally silly like create a billion objects all at once, then it should be fine :)Basically it's worth doing things in an OOP style if that is the dominant idiom for your language of choice.
Gian
+1  A: 

Writing PHP in an OOP manner really doesn't have a noticable, if any, effect on execution time. So nothing to worry about there. However, the advantages are many:

  • Code Modularity and Reusability
  • Code Readability
  • Debugging is way easier as you can collect like functions into objects and if you structure your filesystem appropriately, bugs will be easier to find as your project's size increases.
  • Down the line modifications are way easier if your code is modular
  • Easier for other developers to come in and wrap their head around your code as things are easier to find (of course commenting helps this a lot as well).

Just a few thoughts.

Overall most projects could substantially benefit from OOP design. There may be a few exceptions, maybe on a very small script.

Additionally I would suggest looking into frameworks, as they further the advantages I have described and add more!

jordanstephens