简体   繁体   中英

Why should I start writing object-oriented code in 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?

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.

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 eg on this Stack Overflow question .

I think you want to know, when and why you would fail with your coding behavior:

  • This would be at the moment when you try to work with another person together . The other programmer would never find your code readable . He will take a long time till he understands how your software works.
  • I think it's hard to separate tasks in your code for teamwork . How are your files separated, and how is the naming convention? You have to solve this by your own and don't reuse every known pattern.
  • What are you doing with third-party stuff? How do you integrate them? I do not know any usable library without using an OOP schema...

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...

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:

  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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM