Category Archives: Web Development

Blazing fast WordPress with Nginx and Memcached

Inspired by Eric Mann’s post on caching WordPress with Redis, I thought I’d experiment with a similar setup using Memcached. Any memory caching system should work just as well, but I’ve chosen Memcached because it’s already running on my server and because PHP already has a built-in libmemcached API.

My current setup is Nginx and PHP-FPM, with WP Super Cache. The cache is saved to the filesystem, allowing Nginx to serve static files (which it is very good at) without needing to pass any requests to PHP. This setup has worked very well, so I’ll be using it as a baseline.

To use Memcached, every request needs to be passed to PHP. My gut feeling was that this would be slower than serving static files with Nginx due to the overhead of spinning up a PHP process for each request.

Benchmarks

To find out which of the two setups was faster, I measured the following metrics using WebPagetest and Blitz:

Continue reading

Setting up your editor

Setting up your editor correctly can make working with other developers much less painful (for everybody). Below are some things that I believe every developer should do when editing source code. Any good IDE or editor should have settings to do these things automatically – the points below are paired with their Sublime Text 2 setting.

  • Trim trailing whitespace – "trim_trailing_white_space_on_save": true
  • Always use Unix line endings (LF) - "default_line_ending": "unix"
  • Ensure files end with a new line – "ensure_newline_at_eof_on_save": true
  • Automatically detect indentation style – "detect_indentation": true
  • Or, failing the above, have a way to quickly switch between indentation styles.

Sublime Text packages for web development

Coming from PhpStorm (a full-featured IDE), I felt that Sublime Text was missing a few useful features. Luckily, one of the great things about Sublime is that it can be easily extended with plugins and packages. Perhaps the most useful package for Sublime is Sublime Package Control, which allows you to easily install and manage packages (it can even uninstall itself – über meta).

Below are some Sublime Text packages that I have found to be useful for web development.

  • All Autocomplete extends the Sublime Text autocompletion to find matches in all open files.
  • ApacheConf.tmLanguage provides ApacheConf syntax highlighting (for .htaccess, vhosts, etc).
  • DocBlockr simplifies and automates writing DocBlock comments in many languages including PHP and Javascript.
  • Sass and LESS both provide syntax highlighting for the Sass and LESS dynamic stylesheet languages. Compass also provides a watch/build system for Sass.
  • SideBarEnhancements provides many useful enhancements to the default Sublime Text sidebar, including Copy Path and Open With…
  • SublimeCodeIntel is a full-featured code intelligence engine that provides smart autocomplete and jump-to-symbol functionality.
  • SublimeLinter automatically runs your code through a linter and highlights lines that it deems to contain (potential) errors. SublimeLinter has built-in linters for most popular languages.

Bcrypt: Choosing a Work Factor

Bcrypt is a Blowfish-based hashing algorithm which is commonly used for password hashing because of its potentially expensive key setup phase. A Bcrypt hash has the following structure:

$2a$(2 chars work)$(22 chars salt)(31 chars hash)

The reason that the key setup phase can be potentially expensive is because it is run 2work times. As password hashing is usually associated with common tasks like logging a user into a system, it’s important to find the right balance between security and performance. Using a high work factor makes it incredibly difficult to execute a brute-force attack, but can put unnecessary load on the system.

Using Marco Arment’s PHP Bcrypt class, I performed some benchmarks to determine how long it takes to hash a string with various work factors: Continue reading

Simple Nested Sets in Doctrine 2

Unlike Doctrine 1 with it’s NestedSet behaviour, there is no nested set functionality in the core of Doctrine 2. There are a few extensions available that offer nested set support:

I tried all of these extensions, but none of them felt simple or lightweight enough for my application. What I wanted to do was have a Category entity which could have a tree of sub-categories, e.g: Continue reading

Writing good code is easy

Everybody wants to write “good code”, right? So why is it that nearly every time we pick up another developer’s work, our WTF-o-meter goes crazy?

Everybody has a different idea of what “good code” is. Below are a few ways that I believe we can increase the quality of our code and reduce the number of WTFs our code generates.

  • Keep it simple; refactor overly-complex methods…
  • …Or if refactoring isn’t feasible, document complex methods.
  • Use descriptive variable and method names.
  • Follow code conventions.
  • Don’t commit unfinished or broken code.

Most of these are just common sense. The trouble is, we throw good coding practices – and common sense – out the window when we’re under pressure from things like slipping deadlines and scope creep. If you ever find this happening, just remember to write your code as if the person who has to maintain it is a violent psychopath who knows where you live. What would you rather: miss a deadline, or be hacked up into little pieces by an angry developer?

Why you should date a web developer

Girls always say to me, “I want a really great boyfriend, where do I find one?” The answer is simple: you should date a web developer! Why, you ask? There are dozens, even hundreds of good reasons why you should date a web developer:

  1. Web developers are good with computers

… So what are you waiting for? Go out and grab yourself a hunky web developer!

Convert print and echo statements to short syntax

It’s debatable whether or not it’s good practice to use short syntax in PHP. I personally prefer to use short syntax because it keeps my view files looking tidy.

The regular expression below will find all one-liner print and echo statements (e.g. <?php print $var; ?>) and convert them to <?=$var?> statements. It will not match statements containing closing brackets, for example when using ternary operators: <?=($foo == $bar) ? 'Foobar' : 'Foo'?>

Find:
<\?php[\s]*(print|echo)[\s]*\(?([^>\)]+?)\)?[\s]*;?[\s]*\?>
Replace:
<?=$2?>

Useful Doctrine 2 Console Commands

Doctrine 2′s console is really powerful when you know how to use it. You can generate entity classes and their method stubs, reverse-engineer a database, validate your entity schemas, and much more. In this post, I’m going to cover some of the Doctrine console’s more useful commands and explain how you can use them to reduce development time. For a full overview of the Doctrine 2 console, read the Doctrine Tools documentation. Continue reading