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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With