Christer’s blog o’ fun

July 6, 2009

John Bachar: 1957 – 2009

Filed under: Climbing — christer @ 9:57 am

UKClimbing reports that climbing Legend John Bachar died yesterday in a climbing accident.

John Bachar fell whilst soloing at the Dike Wall, Mammoth Lakes, California yesterday and was found dead at the base of the cliff. He leaves a son, Tyrus. John was 51.

–UKClimbing

Leave your condolences at this Supertopo forum thread: John Bachar – In memory of a great man 1957 – 2009

June 23, 2009

Who implemented this method?!

Filed under: Technology — Tags: , , — christer @ 9:21 am

It’s been a while since I wrote anything on this blog, so I thought it was about time to get back on the scene! Today I’ll write a post about how to figure out which class actually implemented a method in PHP.

Consider the following classes:


<?php
abstract class My_Plugin_Abstract {
    public function onLoad() {}
    public function onConnect() {}
    public function onTick() {}
    public function onExit() {}
}

class My_Plugin_Something extends My_Plugin_Abstract {
    public function onLoad() {
        // Do something magic on load
    }
}

class My_Plugin_Magic extends My_Plugin_Abstract {
    public function onTick() {
        // Do some magic on every tick
    }
}

class My_Plugin_End extends My_Plugin_Abstract {
    public function onExit() {
        // Do something fun on exit
    }
}

As you can see we have an abstract class with some non-abstract methods. The plugins can then choose which of the methods to implement.

Now, let’s imagine that we have an application that runs in a loop. In each iteration of the loop we will fetch an event that the plugins can hook onto. As you can see from the code above the plugins only implement one method each, and not all four that the abstract plugin has. Since we might only want to run the methods that is actually implemented in a plugin we need to figure out which class that implemented it; the plugin itself or the abstract plugin.

PHP has a couple of functions that does something similar: method_exists() and get_class_methods(). The problem is that whenever a class extends another one, all methods of the parent class is available in the child class (with some exceptions), so method_exists() for instance will return boolean true even if the child did not implement the method.

To be able to do what we want we have to use the reflection API. By using this we can “reverse-engineer” our classes and find out some more about which methods to execute.

Back to our application.


<?php
// Register plugins and use the class names as keys
$plugins = array();
$plugins['My_Plugin_Something'] = new My_Plugin_Something();
$plugins['My_Plugin_Magic'] = new My_Plugin_Magic();
$plugins['My_Plugin_End'] = new My_Plugin_End();

// Main loop
while (true) {
    // Fetch an event
    $event = magicFunctionThatReturnsAnEvent(); // Imagine this returns "Load", "Connect", "Tick" or "Exit"

    // Method to execute
    $method = 'on' . $event;

    // Loop through the plugins and run the current method
    foreach ($plugins as $className => $plugin) {
        $plugin->$method();
    }
}

Imagine that the loop runs 4 times, and each time we get a different event. This means that the following function calls have been generated:

My_Plugin_Something::onLoad
My_Plugin_Magic::onLoad // This is implemented
My_Plugin_End::onLoad
My_Plugin_Something::onConnect
My_Plugin_Magic::onConnect
My_Plugin_End::onConnect
My_Plugin_Something::onTick // This is implemented
My_Plugin_Magic::onTick
My_Plugin_End::onTick
My_Plugin_Something::onExit
My_Plugin_Magic::onExit // This is implemented
My_Plugin_End::onExit

Out of 12 function calls, only three are of use. If we extend the foreach loop a little, we can do something like this:


foreach ($plugins as $className => $plugin) {
    $ref = new ReflectionMethod($className, $method);
    if ($ref->getDeclaringClass()->name === $className) {
        $plugin->$method();
    }
}

First we make a ReflectionMethod object by specifying the name of the plugin class and then the method. The reflection object gives us loads of methods to use, but the one we are interested in is the one called getDeclaringClass() which gives us a ReflectionClass object that is a reflection of the class that declared the method. In most cases above the declaring class is My_Plugin_Abstract which only holds placeholders for the methods that the plugins can implement.

If we run our application again the following methods are run:

My_Plugin_Magic::onLoad // This is implemented
My_Plugin_Something::onTick // This is implemented
My_Plugin_Magic::onExit // This is implemented

Looks good! But (there had to be one right?), using the reflection API costs quite a bit. In most cases it’s faster just to make all the function calls above instead of using reflection to check the declaring class and so forth. In some cases it might be worth using reflection for this, and if you have such a case at least you know how to do it. You can thank me later!

May 12, 2009

Two new sets at my flickr page

Filed under: Photography — Tags: , — christer @ 12:26 pm

Agia Pelagia, April 2009
Agia Pelagia, April 2009

Birthday party in Gothenburg, May 2009
Birthday party in Gothenburg, May 2009

April 21, 2009

Two “new” sets at my flickr account

Filed under: Photography — Tags: , — christer @ 9:46 am

Just made some more pictures public in my flickr account:

Gardermoen Raceway, April 2009
IMG_4727

Barcelona/Madrid, January 2009
Building

AOTW 16, 2009: Strife – In this Defiance

Filed under: AOTW, Music — Tags: , , — christer @ 9:45 am

This week I have chosen one of my favorite hardcore albums: In this Defiance by Strife. Listning to this album now makes me want to go to a hardcore show and scream my heart out.

My favorite tracks are:

  • Waiting
  • Will to die
  • To and end

AOTW 15, 2009: Opeth – Blacwater Park

Filed under: AOTW, Music — Tags: , , — christer @ 9:45 am

Another Opeth album on the list: Blacwater Park.

My favorite tracks:

  • The Leper Affinity
  • Bleak
  • The Drapery Falls

AOTW 14, 2009: Deftones – Around the Fur

Filed under: AOTW, Music — Tags: , , — christer @ 9:45 am

The award this week went to Deftones with their Around the Fur album.

My favorite tracks on this album:

  • My own summer
  • Around the Fur
  • Headup

AOTW 13, 2009: Sepultura – Chaos A.D.

Filed under: AOTW, Music — Tags: , , — christer @ 9:41 am

Chaos A.D. from Sepultura got the award for week 13.

My favorite tracks on this album:

  • Refuse/Resist
  • Territory
  • Amen

AOTW 12, 2009: Creedence Clearwater Revival – Cosmo’s Factory

Filed under: AOTW, Music — Tags: , , — christer @ 9:39 am

The award for week 12 went to Creedence Clearwater Revival’s album, Cosmo’s Factory.

My three favorite tracks are:

  • Born on the Bayou
  • Ramble tamble
  • Up around the bend

April 15, 2009

Grenserittet 2009

Filed under: Cycling — Tags: , , — christer @ 10:58 am

Yesterday I signed up for Grenserittet, an 80km bicycle race from Strømstad, Sweden to Halden, Norway. I was supposed to attend last year as well but was too late.

Older Posts »

Blog at WordPress.com.