craig campbell

  • added July 24, 2010 at 2:22 pm
    Most PHP developers are probably well aware of this already, but the release of PHP 5.3 brought a few cool features to the language including namespaces, late static binding, and anonymous functions (closures).

    In the interest of keeping this brief I just wanted to provide a few simple examples.

    Let's imagine you have an array of data:
    $users = array(
                 array('id' => 1'name' => 'Tom''birthdate' => '1980-05-05'),
                 array('id' => 2'name' => 'Julia''birthdate' => '1985-07-07'),
                 array('id' => 3'name' => 'Michael''birthdate' => '1974-11-17')
             );
    Now let's say you wanted to grab all the user ids from this array. Normally you would do something like this:
    $ids = array();
    foreach ($users as $user) {
        $ids[] = $user['id'];
    }
    With anonymous functions you could achieve the same thing by doing:
    $ids = array_map(function ($user) {
        return $user['id'];
    }, $users);
    read more
  • added June 30, 2010 at 10:38 pm
    updated July 1, 2010 at 8:56 pm
    I signed up for GitHub a little while ago, but hadn't used the site for anything until just now.

    I want to be more open about sharing my code with the world, and I was inspired by the irony of posting a Subversion utility script on GitHub.

    As far as what the script does, it is for finding what revisions in one branch are missing in another.
    I will give a brief overview:

    Let's say you are developing a product in trunk and it is time for the 1.2 release. You create a 1.2 branch from trunk, push it out to the world, and begin developing version 1.3 in trunk. After 1.2 is released you start getting tons of bug reports. You work feverishly to fix the bugs in the 1.2 branch but sometimes forget to merge your bugfixes back into trunk. A month later, you are about ready to release version 1.3 from trunk, but some of the bugs you have already fixed in the 1.2 branch were never merged back into trunk. If they don't get merged you risk reintroducing bugs you have already fixed!

    How on earth are you going to find out what revisions you forgot to merge?
    read more
  • added May 31, 2010 at 1:08 pm
    I use jQuery as my javascript framework of choice because I love how simple it is, and how much you can do with just a single line of code.

    I decided it would be a good idea to share some jQuery code snippets to do common tasks. So hopefully there will be plenty of posts where this one came from.

    The easiest way to open a link in a new window is to do something like this:
    <a href="http://www.google.com" target="_blank">search the web</a>

    Unfortunately, the target attribute is not valid in XHTML strict so you have to use javascript if you want your pages to validate.
    read more
  • added May 24, 2010 at 1:43 am
    When I work locally on projects I like to use symlinks for all of my shared libraries. It makes it way easier than having to manage a million different versions of the same files.

    Recently I was creating a tarball (.tar) archive to deploy somewhere using the -h option to follow symlinks like so:
    tar cvfh code.tar code

    I noticed that many of the symlinked files that were copied into the archive ended up being transformed into apple double files. Of course, this is quite annoying, and I wanted to get rid of them. I did some googling and found the answer buried somewhere on the internets so I thought I'd make it easier to find.
    read more