Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I start writing object-oriented code in PHP?

Tags:

oop

php

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?

like image 333
ppp Avatar asked Feb 26 '12 09:02

ppp


2 Answers

OOP was made to make programming languages more similar to real life.

What does that mean?

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.

How does it help me?

Well, there are several things:

  • It makes your code more maintainable. Instead of dividing your program into tasks (functions), you divide it into objects, if you think of a database connection as an object (meaning, there can be multiple database connections, they share methods and properties, but each is preformed on a different instance), it makes it easier to understand and maintain.
  • It makes your code more readable. You define an object with the class decleration, and then call it with the new ClassName() keyword.
  • It allows for extensibility and flexibility. Just like KitchenAppliance can be extended into Oven or Stove, so can your objects and classes.

Summary

OOP programming comes with many advantages. It requires a slightly different way of thinking, but eventually, it's worth it.

like image 114
Madara's Ghost Avatar answered Nov 15 '22 13:11

Madara's Ghost


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:

  • Strategy pattern (http://en.wikipedia.org/wiki/Strategy_pattern) - for using different alghoritms/solutions in class without changing it
  • Observer pattern (http://en.wikipedia.org/wiki/Observer_pattern) - you can invoke different actions (and register them during execution) - when state of object changes.
  • Decorator pattern (http://en.wikipedia.org/wiki/Decorator_pattern) - you can extend your object dynamically, and use new objects in same manner as old.

Franky speaking, if you want to better understand OOP, you have to:

  1. Learn or understand common design pattern.
  2. Start using unit testing, you will find out that lack of dependency injection can be real pain in bad architecture.
  3. learn and understand OOP principles, like SOLID http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29.

Without this you will be using functions encapsulated in classess, like in namespace, not OOP.

like image 3
Slawek Avatar answered Nov 15 '22 13:11

Slawek