Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

The day I have been dreading has arrived. I never felt OOP or good software design was important(I knew they were important, but I thought I could manage without them.). However having read otherwise almost everywhere on the interwebs, I started dreading the day when my client would ask me for new features in an existing app. The day has come and the pain is unbearable!

I have never coded my PHP websites "properly"(PHP is my primary language and the bulk of my work. I am learning Python (using web2py)) I take care that the website doesn't fall apart in a daily use scenario. I code pages like I was creating a list of static html files with bits of "magic code" in each of them(this bugs me a lot). How do I make the whole app more or less a single object? For eg. How do I design the object model for an invoicing app?

I use a lot of functions for doing any particular thing in the same fashion throughout the app(for eg. validation, generating ids, calculating taxes etc.).

I know the basics of OOP in general. Can anyone point me to source code samples of functional apps written in php? Or can someone provide pointers so I can recode my existing apps in a more modular way.

share|improve this question
    
Sounds to me like your app needs some design pattern. MVC is fairly reasonable in the web world as you have a view which is your client browser rendering, your controller which is server side code and some data representation usually a database which is your model. –  Chris Feb 3 '11 at 16:53
    
I have tried using CodeIgniter for apps. But I kept doing with views, what I should've been doing with controllers and vice versa. I have relapsed back to plain php. I think I should revisit CI. –  abel Feb 3 '11 at 17:05
add comment

3 Answers

up vote 3 down vote accepted

Check the Symfony framework and Doctrine ORM, those are the best coded and most well-architected PHP projects that I know of.

Early Software Architecture

Also, get familiar with design patterns, check FluffyCat's PHP Design Patterns page and check each pattern in wikipedia so you gain better insight to what they are for. In general, design patterns solutions to frequent problems (inherent to the language, and mostly object oriented), for example, the Composite design pattern helps you deal with nested categorization of stuff.

Good luck and welcome to early software architecture.

share|improve this answer
add comment

I think the most important step (or at least the step with the highest return-on-investment) is to begin using a framework (pre-built or one you've written) with a single entry point1

This is the best way to practice DRY (Don't Repeat Yourself) in PHP and will help you realize separation of processing code and display code.

This will also allow you to begin to see how OO can actually be used in PHP. Right now, Objects are hard for your to imagine because of all the "magic" code, as you call it. Request handling and business logic (where OO is well suited) are very different things. Request handling can be done with OO but it's not absolutely necessary, and the objects will be distinctly different from the objects you need in your application.

Once your request-handling is separated and following a distinct and consistent pattern, Objects related to your application will fall naturally into the controller methods without mixing into the request-handling, which would make your code feel cluttered.

When doing this, set up a good __autoload function so that you don't have to import every class in your app for every request. Classes won't be loaded until needed.


1: Something like the framework I described in an answer about htaccess and one on StackOverflow about a good lightweight and clean framework

share|improve this answer
add comment

I would recommend Zend Framework which is based on Model-View-Controller architecture.

Start with quick start or pick one of many tutorials.

Some of the key features of Zend Framework (from Wikipedia):

  • All components are fully object-oriented PHP 5
  • Use-at-will architecture with loosely coupled components and minimal interdependencies
  • Extensible MVC implementation supporting layouts and PHP-based templates by default
  • Support for multiple database systems and vendors
  • Email composition, delivery and retrieval
  • Flexible caching sub-system with support for many types of backends, such as memory or a file system.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.