Reputation: 2055
I have been using regular PHP for some time now. My formal code training is zero. Whatever I've learned I've found here, on the PHP documentation site, the MySQL documentation, etc.
I write PHP from scratch. I use functions for tasks that re-occur, I apply MVC to write more maintainable code, and I recently wrote a nice little library with some of my functions so I can save time in future projects. Long story short, without being some sort of guru, I have a decent relationship with PHP, and so far it seems to get things done for me.
So my questions are the following: Why should I start writing object-oriented code in PHP? How will it make my programming life better and why is it better than the traditional way of doing things?
Upvotes: 8
Views: 2565
Reputation: 9319
With OO you can develop applications a lot faster and in a cleaner way. You could easy to reuse your extisting classes with extending them (reducing your code base).
With OO design you only have to deal with small pieces of codes at any one time, not a bunch a functions in a file with 3000+ lines of code. You should also look after the SOLID guidelines.
Upvotes: 0
Reputation: 3909
For sure you can write your code without OOP and try to implement designs/patterns like MVC without using just a single object.
I don't want to answer why to program in OOP. This you could read in e.g. on this Stack Overflow question.
I think you want to know, when and why you would fail with your coding behavior:
There are many more problems which are surely possible to solve, but every time you lose the possibility for others to understand your code and to reuse it in other programs...
Upvotes: 3
Reputation: 174957
OOP was made to make programming languages more similar to real life.
We live in a world of objects. You are an object (Person
), you live in an object House
, that House
object (as well as any other House
object) has an House::$address
and House::$number
, your house probably contains other objects such as LivingRoom
and Kitchen
. The Kitchen
can hold Oven
and Stove
and Refrigerator
, which are all extensions of the KitchenAppliance
object.
OOP programming takes that approach, and incorporates it into the programming world.
Well, there are several things:
class
decleration, and then call it with the new ClassName()
keyword.KitchenAppliance
can be extended into Oven
or Stove
, so can your objects and classes.OOP programming comes with many advantages. It requires a slightly different way of thinking, but eventually, it's worth it.
Upvotes: 9
Reputation: 583
You have received a lot of comprehensive answers, so I will use one argument: design patterns. (http://en.wikipedia.org/wiki/Software_design_pattern).
You can find tones of solutions for commons problem, which can save your time and improve quality of your code.
Some design patterns examples:
Franky speaking, if you want to better understand OOP, you have to:
Without this you will be using functions encapsulated in classess, like in namespace, not OOP.
Upvotes: 3
Reputation: 1299
One word: cohesion.
When you start developing software using objects (especially when those objects use Dependency Injection), you find that common functionality starts to gravitate into their own specialised classes that are reused. This makes maintaining and enhancing the software MUCH easier, and results in higher quality software.
Example:
Many applications use sessions, for storing all sorts of stuff. When all session data is managed by a specialised session manager class, all the code that is responsible for dealing with the session is kept in one place. If you want to change the way you application uses session data (perhaps to make it more secure, or more efficient), you only need to change code in one place.
I made the jump to OOP PHP three months ago and it is one of the best things I have done.
I started off with PHP Object-Orientated Solutions and have just finished Real-world Solutions for Developing High-quality PHP Frameworks and Applications. Both of those books have helped me a lot, and I highly recommend them.
The learning curve is quite high. But I guarantee, you will be glad you've turned to OOP.
Upvotes: 2