Experiences of Using PHP in Large Websites

Disclaimer – I haven’t read this yet, I’m heading out the door and home to make myself some dinner! I see that this paper is from 2002 but when the author recommended using Perl I had to link this and read it later! Cracked me up. hehe.

… few languages are as bad as PHP for doing serious development work. The author and his colleagues have had good results with Perl, and believe that languages such as C++, Java, and Python should serve equally well.

PHP, MVC And Smarty Caching

Following on from previous posts on PHP and MVC and in particular on using Smarty caching in the MVC design pattern, I used Smarty to cache the output of an app at work this morning.
I agonised in my last post about where to put the caching, should it go into the viewer, or the model, or the controller?

  • I finally decided to make the viewer responsilble for deciding if the template is cached or not.
  • The controller then asks the viewer if the current page is cached, if it’s not, then it runs whatever functions the model needs to generate the page.

class mvcViewer
{
  function mvcViewer()
  {
    $this->cacheKey = md5( $_SERVER[ 'REQUEST_URI' ] );
  }

  function display()
  {
    // modified to use the cacheKey.
    $this->assign();
    $this->smarty->display( $this->formTplName . ".tpl", $this->cacheKey );
  }

  function is_cached()
  {
    return $this->smarty->is_cached( $this->formTplName . ".tpl", $this->cacheKey );
  }
}

class mvcController
{
  function main()
  {
    switch( $this->page )
    {
    default:
    $this->model = new mvcModel( $form );
    $this->view = new mvcView( $this->model, "mvc-Index" );
    if( $this->view->is_cached() == false )
    {
      $this->model->doSomethingHere();
    }
    break;
    }
  }
}

The beauty of this is that the model doesn’t need to know about the caching at all. The developer can concentrate on the business logic of his application without worrying about the underlying architecture. That’s one reason for keeping the cache key in the viewer too. Don’t lets confuse the developer!
Of course, this presumes the most simplistic caching system, but it would suffice for most cases!

Switching from PHP to Zope/Python

Now here’s an interesting discussion on kuro5hin.org
I’ve always wanted to look at Zope, as it’s based on Python I thought it’d offer me lovely OO programming and fast development. Reading some of the comments here I have to wonder, especially in light of Russell’s rant about the complexity of Java. (Picked up by Keith who might be a deceptive right-wing troll but he writes some good techie stuff!)
But then I think there’s room for both types – pure computer scientists who will most likely drive future ideas, and those that get the job done in the real world.

PHP Newsletter software

PHPList was mentioned on various blogs a few weeks back and we’ve used it here a few times. I’m not 100% happy with it however as it’s missing some glaring features and those make sending emails very difficult.
I searched freshmeat.net earlier today and installed LetterIt, a similar piece of software from Germany.
It’s a fairly simple program, doesn’t look very flash, but it does work well, and we just sent out a mailing to over 1,000 customers this afternoon with it.

WordPress and Smarty

I was drafted in to help on WordPress a long time ago. Unfortunately I haven’t done much work on any OSS projects in a while and it was bothering me.
I did do some useful work though – I found out that the Smarty register_resource construct was buggy when using caching. That rules out using MySQL as a backend to hold templates. (I posted a mail to the Smarty list but nothing came of it. *shrug*)
Finally, after re-installing Linux on my new hard drive last weekend I Smartyised WP! Here’s a WIP snapshot.
What does it do? Well, I wrapped some of the Smarty template API (ie. bloginfo(), etc) in Smarty functions of the same names. I’ve only done enough to get the default template working, as it’s laborious work and I wanted to get something out quickly!
The template is split up into 3 parts: top.tpl, post.tpl and end.tpl. They live in users/main/templates/ and can be edited there with a text editor. The online editor from b2++ will make it’s way there eventually probably.

Before trying this, please be sure to backup your WP installation. The files in the tarball don’t conflict with the files in CVS but I can’t guarantee that this won’t delete all your WP install files if you’re not careful!

Download the WP – Smarty mod. (40Kb)