Categories
PHP Programming

OOP Best Practices and architecture

  1. When creating classes, make a really general abstaction of the object you have in mind (usually an abstact class or interface), and then start working your way down to the specifics using inheritance, encapsulation and polymorphism. For example: to create a book object for an ecommerce site, create something like Product / Item / Books / Book.
    • Advantages: This way your architecture can accomodate other objects later (cars / clothes / other categories)
    • Inside your classes, you avoid using hard to maintain “If” flags and clauses to perform different tasks per type of class
    • At the same time, you maintain a hierarquical structure of your objects that help you control variables and methods per level.
    • Make sure your classes are not overloaded with responsibilities. Spread responsibilities around the hierarquical structure of classes, if you can’t define a class responsibility with 25 words, and without using “and” and “or” words, you may be overloading your class with tasks. This will be hard to maintain.
    • But always remember: OOP best practices are not set on stone. If you have a good reason to bend the rules (performance, etc), do it, but think hard before you go ahead.
  2. As a general safety rule, you can set all methods and variables to private, and start resetting them to protected or public as you go along.
  3. Add error handling to all places things can go wrong (unexpected input / output usually). See PHP Error Handling section for more details, but basically extend the provided Exception class with a new class for each kind of error, and at the catch part of your try catch statement, have several catches that will deal with the particular error the proper way based on Exception type detection.
  4. Here are four possible indications of design problems:
    1. Code duplication: if you get a deja vu feeling at code writing a class, think: if I change something fundamental in this routine, will the other simmilar routine need ammendments? If so, then you may have a dup class you can compress in one.
    2. If a class depends too much on Globals variables and its context, this class will be hard to maintain. Try to make each class its own environment.
    3. If a class is doing too much, think of ways to reduce responsibility and extend down to other classes.
    4. Testing for the same condition with “if” statements throughout diffent classes may call to review your polymorphism. Where you can do the “if” test once, and spawn different objects with no “if” conditions down that fork
  5. Inside your classes, seek for those lines of code that express variation and fork the behavior of the current class given different values or conditions, and try to encapsulate them in their own type class.
  6. The golden rules: “Do the simplest thing that work”, and “are you sure you are going to need this extra complexity?”
  7. Avoid global variables like the plague, they make your classes highly coupled with the system, can’t be reused easily, and it’s hard to encapsulate stuff when using those. If you are to use Globals (say, to avoid passing too many variables or objects around) use singletons instead.
  8. Use phpDocumentor to add documentation to your code, even the default, do nothing and just run it option is better than nothing: http://www.phpdoc.org/
  9. Make sure you include Unit testing with PHPUnit

Leave a Reply

Your email address will not be published. Required fields are marked *