• 5
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

Singleton, Decorator, Abstract, Factory, and the list goes on. How relevant are OO design patterns in developing PHP applications for the web? Does it do anything for performance? Or is it just to keep code lean for agile development practices? Who is the major benefactor for implementing these design patterns? Is it the customer or the developer?

I realize I am asking multiple questions, but they all relate to the same topic. I am not certain there is a necessity for OO design patterns with a scripting language since it is compiled at run time. What do you all think? Is it important?

Design patterns are created to solve specific problems. These problems occur whether you use PHP or any other language (though the patterns may differ by language as well). Most of the patterns have their roots in object-oriented design, but can be adapted to procedural settings. Use the design pattern when you have a problem that the pattern addresses, whether PHP or any other language. Don't use a design pattern just because it's a "design pattern" -- know how and when it applies and when it doesn't.

Having said that, much of what design patterns do, is organize code to accomplish their purpose in a clean, understandable way. There are other ways to solve the problem, but the design pattern is a well-recognized, clear, and understandable method. If you do need to solve a problem addressed by a particular pattern, you would usually be best off using the pattern (or adapting the pattern) rather than choosing an alternative.

Using patterns, where appropriate, is thus beneficial to both current and future developers -- cleaner, better understood code -- and the customer -- less time reinventing the wheel, more robust, maintainable code. Using patterns when they aren't appropriate benefits no one; it's potentially an exercise in frustration trying to adapt a pattern to solve an unrelated problem and more than likely will simply make things more complicated.

  • 10
Reply Report
      • 1
    • Great answer! is there anywhere on the web where you can find tutorials on learning to recognize the patterns and when to use them? What is the best way to learn them?
      • 1
    • There's a book by the PHP|Architect guys on Design Patterns in PHP, which I've found helpful since it explains how each of the patterns works in the context of the language. Learning to recognise what they are and when to use them... that's a bit harder, it just comes with practice.

How relevant are OO design patterns in developing PHP applications for the web?

IMO they are very relevant for any non-trivial PHP application, as long as the pattern you are using is applicable for the problem at hand.

Does it do anything for performance?

Not necessarily, and you can take that in both the positive and negative respects. Most popular design patterns aren't going to directly add any positive performance benefit, and some that impose more inheritance or class structure might add a negligible performance cost (I emphasize negligible). The benefits of using well-known patterns, when applicable, outweigh these negligible costs.

One example I can think of where a pattern would directly improve "performance" (with regard to memory usage) would be the appropriate use of the singleton pattern. If you really only need one instance of an object at any given time, then you minimize memory usage by using that instance.

Or is it just to keep code lean for agile development practices?

I would say that correct use of patterns keeps code more maintainable, rather than "lean." This would facilitate any development cycle, including agile, since code that uses well-known patterns is easier to read.

Who is the major benefactor for implementing these design patterns? Is it the customer or the developer?

Primarily the developer (especially if it is a plural developer(s) where a team has deal to look at the same code.) There is an indirect impact on the customer in that well-written code will obviously have less bugs, and software that uses patterns will probably have less turnaround time from re-inventing the wheel.

  • 3
Reply Report
      • 1
    • I agree with all of your feedback. Then why is it that so many small to medium "customers" prefer using less-experienced/inexpensive coders? Maybe they just don't care how the guts look as long as it has a big engine and the paint is shiny, eh?
      • 1
    • I think small to medium customers prefer cheaper coders simply because they are cheaper. If given a choice between a cheap developer that will write spaghetti code and a more expensive developer that will write great code, a lot of customers, especially customers that are new to contracting software development, will choose the cheap developer. Good code is hard to quantify. The more experienced SMBs will know a little bit about what to look for, or will at least look heavily at your portfolio.

Yes, but you have to choose the right patterns for the platform. By far the most important OO design pattern is MVC (Model-View-Controller), which all of the major frameworks (CakePHP, CodeIgniter, etc) use.

  • 1
Reply Report
      • 2
    • It can be debated whether it is an architectural or a design pattern, but MVC is such a fundamental pattern for modern web development - to organize code, make it more modular, maintainable, etc...
    • I am a huge fan of CakePHP and MVC in general. I believe it brings structure to your code base for sure. But is MVC a design pattern or an architecture? Or is pattern/architecture synonymous?

Using an OOP vs a non-OOP approach there may be a slight difference in performance, with non-OOP being a bit faster, but the difference would be practically negligible.

I think OO design patterns would be beneficial for code organization, but the design issues are left up to you. I think you would benefit more than the user. The user can see the same result whether you use OOP or non-OOP.

  • 1
Reply Report
      • 1
    • Great insight! I have noticed from all of the code I have reviewed in my lifetime that others feel the same. Some code would take more time to ramp up on than to write the updates for once you figured out what was going on in it.
    • Personally, I like the OOP approach. It saves me alot of time trying to change all the code when I can make the changes in a few places.

What algorithms (like quicksort) are to procedural programming, design patterns are to object oriented design. They are proven way to slove some common problems.

Main beneficator is a devolper - but as a side effect a customer will probably obtain better final product.

Whenever they are relevant or not, depends on the certain application requirements and approach you have choosed. It may sound laconic, but when you start to analyse your application and compare its design requirements with existing patterns you will know how and when to use them.

  • 0
Reply Report
      • 1
    • Better in what way? I know many customers who would rather pay a high-school student $500 to paste together their website than to pay an engineer $1500 to build them something spectacular. So how do we get customers to realize they get something "better" by hiring experience?
      • 1
    • Usually object oriented code is more flexible than procedural one and this can be "touched" by the enduser. Of course, good procedural code can behave better than not-so-good object oriented one. Once you are familiar with those patterns, you'll become more productive -> more time to code -> better code -> better final product. But as you said it all depends on customer's requirements.

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions