Posts Tagged ‘coding’

Modernizing Legacy Applications In PHP

Tuesday, December 8th, 2015 | Books, Programming

Modernizing Legacy Applications In PHP is a book by Paul M. Jones on how to bring legacy codebases up to date. Jones is a relatively big-whig in the PHP community, being involved in most of the PSR standards and a contributor to the Zend Framework.

For me, the book was both excellent and uninformative. What I mean by that is that I do not think I learnt anything from reading it. However, I flatter myself that I am pretty good at PHP and have spent many years working with legacy codebases. It was always been an interest of mine, and I have previously submitted conference papers on the subject. I have been doing just what this book describes with my current client.

That in itself shows the quality of the book though. It describes every step I have been going through in a logical and clear order. It explains introducing autoloading, separating out the concerns, adding unit testing and injecting your dependencies. It says you from the tangled mess to a clean and modern application in an easy-to-follow manner. In short, it is pretty much everything I would recommend to someone starting to clean up a legacy application.

It has code examples and some tips and tricks in it, but for the most part it is quite high level. It also deals with PHP 5.0 and onwards. There is perhaps room to expand here. At Buzz I had to come up with techniques to support ancient problems like register globals, and at the NHS I ran into PHP 4.6 installed on their servers. Solving these kind of problems, and the little code tricks you can do, might have been a useful additional also.

Overall, I would recommend this book to anyone who feels the least bit daunted by the idea of modernising a legacy PHP codebase. It is clear, easy-to-follow and takes you through exactly the steps you should follow.

modernizing-legacy-applications-in-php

Testing multiple conditions in if statements

Friday, December 2nd, 2011 | Programming

This is one of the geeks among you. I’m currently working on a new open source project which uses a series of database models derived from a base model which includes some standard methods and I was having one problem in particular to do with updating data values in an object.

if (
    !$object->setName($d["name"]) ||
    !$object->setSlug($d["slug"]) ||
    !$object->setDateByArray($d["date"]) ||
    !$object->setContent($d["content"])
) {
    $this->setMessage($object->getMessage());
    return false;
}

As you can see, this does an if statement and then runs all the updates – the idea being that if any of them come back as false, it can set the error message and return false to the controller.

This worked as expected but with one problem – as soon as one of them appears false, PHP stops looking at the other conditions in the if statement. This is a problem because it means the user only gets one piece of feedback at a time. Whereas, it would be much better if we could give them all the fields that were wrong at once.

However, this doesn’t seem possible in the above implementation. Even if I switched it round to check that everything is true, and everything must be true, that suffers from the same problem that once something isn’t, PHP will stop checking the conditions.

I considered whether I could implement it using exceptions – putting all the checks inside a try block and then have the update operations throw an exception if they were invalid. I could then catch this exception and add the error to the list of errors. However, this suffers from the same problem – as soon as the first invalid value triggers an exception, the rest of the try block is ignored.

The solution I have eventually come to is this:

$writes = array (
    $object->setName($d["name"]),
    $object->setSlug($d["slug"]),
    $object->setDateByArray($d["date"]),
    $object->setContent($d["content"])
);

if (in_array(false, $writes)) {
    $this->setMessage($object->getMessage());
    return false;
}

All the operations are run, and their values are fed into an array. That way all the checks get run no matter what value they return. I then search the array for any values set to false and if any of them were, I know there has been a error and can flag it up with the user.

SocietasPro v0.3

Monday, November 28th, 2011 | Foundation

Just six days after we released version 0.2 of SocietasPro, we’re pleased to announce that we’re announcing the third iteration, which is now available to download on Github.

Here is what we’ve changed in this version:

  • Events can now be displayed on a calendar
  • Added style to the system pages
  • Improved visual editor
  • High contrast admin version added
  • Custom columns are now exported in the CSV
  • You can now change a member’s password
  • You can now reset your own password
  • Version checker added to admin panel
  • Added support for custom selects
  • Members import now supports custom columns
  • HTML Purifier now cleans dangerous HTML
  • Locations are now shown on the events page
  • Mac line endings are now handled on imports
  • Audit trail now works with deleted members
  • Deleting members now cleans up their custom datas as well
  • Duplicate members are now filtered out on imports
  • You now get feedback if your login fails
  • Events are now sorted by date
  • Added extra stats to the control panel

Where will the project be going next? We’re going to put some work into the front end and then it should finally be ready to demo! Stay tuned to our updates on Twitter for further updates.

Gluttons for punishment

Sunday, June 27th, 2010 | Tech

There was a really interesting poll on SitePoint today asking whether freelance web developers were working this weekend. The results were as follows:

  • 43% said they always worked weekends
  • 34% said they sometimes worked weekends
  • 20% said they worked weekends if necessary
  • Only 3% said they never worked weekends

What we can probably assume from this is that on any given weekend, more than half of people who do freelance web development are working. Crazy people. Anyway, I would love to chat more about this but I have code to write…

Calling the Brain Trust

Monday, May 31st, 2010 | Tech

Ok, here is the situation. I have a website which has two virtual hostnames pointed at it – domaim.com and sub.domain.com. They both point to the exact same location in the file system and serve the same content. However I need one of them to invoke a http authentication and the other not to.

The obvious solutions I see are…

Set up a rule in the .htaccess so it only requires a valid user when accessing the sub domain. This is the simplest solution but I don’t think such a rule exists.

Dynamically serve different .htaccess files based on the host being accessed. This ticks neither box of being simple or actually something you can actually make the computer do. But maybe it is if you start layering your .htaccess files.

Invoke the http authentication from the PHP script itself, detecting the server name and serving if appropriate. This is doable and straight forward to implement but requires quite a bit of coding.

Also maybe I can do the original solution, but only inside the vhosts.conf file? I haven’t really looked into that so that’s just speculation. Question is, which is the easiest solution to implement?

Dinner and ‘lancing

Sunday, June 28th, 2009 | Life

Wednesday I headed over to my parents for dinner for what I thought would be a relatively easy evening with plenty of time to sort out my room when I got back home.

As is turns out the NHS had just dumped a load of work suddenly on my previous job, Open Door Desig so I ended up heading over there to give Nick a hand ith it. It was relatively simple stuff to take care of though it was going on 10 when I left there so by the time I got home I didn’t have much time to sort my room out. I have since thankfully so I’m no longer living in a cave of boxes which is nice.

ASP dictionary fun

Thursday, May 7th, 2009 | Programming

ASP has a lot of “interesting” features shall we say. I’ve just been working with the dictionary object which is basically an array with named items, so everything has a key and value pair.

There is a method to add elements to it which accepts variables instead of literal text strings (as you would expect of course) but it doesn’t seem to accept variables in array.

For example, this will not work…

totals.Add RSTdata(“id”), 10
Response.Write(totals(RSTdata(“id”)))

However this will work just fine…

idNumber = RSTdata(“id”)
totals.Add idNumber, 10
Response.Write(totals(idNumber))

This isn’t quite as cool as the way ASP decides the forget about some variables but only after you’ve referenced them for the first time creating a rather nice heisenbug when you put in a few print lines but never the less interesting.

More job based commentary

Tuesday, June 17th, 2008 | Life

It’s week two of my job at Open Door Design and it’s coming along nicely. I’ve been working around with one of their previous big sites which I’ve really been able to get my teeth into and rip apart. It’s everything you expect when you get into the real world, I’ll leave you to fill in what that means :D.