21

I've been thrown head-first into a new job developing web applications in PHP. I'm by no means new to PHP, but I haven't developed large-scale applications before. I'm wondering how to structure my development to avoid setting myself up for problems in the future. How do I design and architect my applications in a sound way that allows them to scale over time in terms of functionality and performance. I'm thinking of things like:

  • Separating back end from front end
  • Directory structures

I would appreciate pointers to architectural and application design patterns, frameworks and methods that allow me to approach large-scale PHP web application development in a sustainable way.

2
  • Hi! The most common architecture for web applications is MVC, for PHP and every other popular web platform. That said, you should read our FAQ. Although software architecture is on topic, you need to revise the question to be a bit more specific. A "healthy discussion going about general architecture" doesn't fit the Q&A format of the site.
    – yannis
    Jan 28, 2012 at 15:47
  • I am a frequent visitor to the S/E Sites, I had a feeling there would be no one answer to this question however, hence the "discussion" comment. Thanks for the MVC pointer though :) Jan 28, 2012 at 16:15

3 Answers 3

33

A rough diagram of the architecture of the latest large scale project I was involved in.

enter image description here

It's only a basic outline, adapted from the actual architecture documents and presented in a way that resembles a typical n-tier approach combined with a typical MVC approach. As you can see the logic and data tiers are connected via a service layer, and more specifically a REST API, that was inspired by Recess, a lesser known PHP framework.

Don't reinvent the wheel

I work with three frameworks:

  • Zend Framework

    The behemoth of PHP frameworks, with an impressively well written codebase and extensive list of features. On large scale applications you'll find yourself tweaking the framework more often than not, and I find ZF's codebase the most pleasant to work with. But beware, it's not an entry level framework.

  • Kohana

    Kohana started out as a fork of CodeIgniter, and that was reason enough for me not to use it, initially. Nowadays it has grown into a solid and elegant framework that differentiates itself from every other by following an Hierarchical MVC approach. HMVC allows for a greater extend of modularization than MVC. For the project in the diagram I adapted Kohana's HMVC to ZF, but I've started using Kohana for smaller projects and considering it for larger as well.

  • CodeIgniter

    I only use it because of a legacy project I inherited, avoid if possible.

As the other answers pointed out, an ORM always comes handy. I use Doctrine extensively, and you should take a look at its brand new mappers for CouchDB and MongoDB. Scalability is a must on large scale applications and you should evaluate NoSQL solutions.

All that said, the important thing to remember is that larger applications usually have unique challenges. You should evaluate every popular third party solution there is, and you will probably gain a lot from a couple of obscure ones. When I first evaluated Recess it was far from production ready but its approach essentially made it into the project.

Performance

On typical websites you may get away with simple output caching and opcode caching but on large scale applications you should really consider memory caching, that most commonly is build around memcached.

xdebug is mostly known as a debugger, but can serve as a profiler as well. I've recently started using Zend Server and I absolutely adore its code tracing features. Unfortunately those are not available in the Community Edition, but xdebug is a pretty decent alternative.

If you are using Apache, make sure to optimize the hell out of it. nginx and lighttpd are apparently better choices, performance wise, but I haven't used them a lot and I can't really say.

As for the database, Doctrine's query & result caching works wonders, especially combined with memcached. And of course, we can't forget about the front end. Yahoo's Exceptional Performance team has assembled an extensive list of best practices. I'm not really a front end developer, but I've seen amazing results on solo projects.

Lastly PHP has a brand new garbage collection mechanism, worth looking into.

Security

The world of PHP security is chaotic, to say the least. I'm no expert, so treat the following as generic tips:

  • Open Web Application Security Project

    Lot's of good stuff in there, but for a quick overview you should start with the top ten list. And research PHP solutions for those common vulnerabilities.

  • Stack vulnerabilities

    A good habit is to periodically monitor PHP's open bugs. Even if you are no expert yourself, there are almost always workaround tips on security threats. And of course, you should extend the habit to every other part of the stack, especially the most vulnerable ones, like the web server and the database.

The crowd over at IT Security Stack Exchange can help you with more educated answers.

Further reading

11
  • 1
    Great answer; great presentation; great layout... Great Job!
    – Dynamic
    Jan 28, 2012 at 23:04
  • @Jae Thanks! Did you read all of it? :P It's a bit long, I was wondering if I should prune it a bit.
    – yannis
    Jan 28, 2012 at 23:06
  • Ha, I tried! ;-)
    – Dynamic
    Jan 28, 2012 at 23:24
  • Actually Yannis, I've got a question for you. I've recently grown very interested with PHP. I've been trying to learn it myself, but I'm having a bit of trouble. It also seems you're pretty good with PHP yourself :-). Would you mind sharing a bit of guidance? If so, I'll create a chat room.
    – Dynamic
    Jan 28, 2012 at 23:28
  • Thats exactly the sort of answer I was looking for! Its a hell of a lot of info to take in, I think ill have to do some absorbing and tinkering over the next hour but you've given me somewhere to start. Thanks :) Jan 28, 2012 at 23:41
1

IBM's developerworks web site has a huge pile of PHP articles, a lot of them quite good. They have a series of articles comparing PHP web frameworks, and another series about web sites using the CakePHP framework.

O'Reilly's old "Onlamp" website has an article about MVC in PHP. The author of that article went on to explain MVC in some detail.

The O'Reilly articles are a little old, but they'll get you going. The IBM developerworks stuff is really good, and covers a lot of what you're asking for.

1

I've been working around PHP for a few years now. Altough I agree with Yannis that this question is somehow open, I think I'd be giving you a few pointers. First, as Yannis said, yu should look into MVC, to do so, the two frameworks I can recommend are CodeIgniter and Symfony. The first one is lightweight and very easy to get started with, however, you might just need to add some extra customizations to get a nice setup working, will get into that soon. Symfony is a project started by Fabien Potencier that uses many of the design patterns in software engineering, however, the learning curve is much steeper than CodeIgniter's.

Now, second, you should look into database connection, which takes me into the two most prominent ORM frameworks for PHP, Doctrine and Propel. Personally I love Propel and even wrote on how to set up a clean Propel install on a CodeIgniter based app, however, Symfony is more into Doctrine, but let's you use either one. If you'd like to check out some more on Doctrine and Propel, take a look to this question I asked some time ago.

Finally, you should look into a templating framework, like Smarty, Dwoo or Twigg. Smarty is the oldest and hence the most stable. Dwoo inherits from Smarty and adds a thing or two to support OOP better on PHP 5. Finally Twigg is the templating alternative provided the Symfony team, I haven't see it myself but if it comes from the Symfony team it should be nice.

Hope this whole speach makes some sense, David

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