Archive for April, 2009

Usenet copyright violations and Wolverine

Wednesday, April 8th, 2009

My usenet provider has deleted all binary articles relating to the recently leaked Wolverine work print. This is interesting because it hasn’t deleted terabytes of other data that infringes on other copyrighted works. Why not?

Either the copyright holders or their agents can’t/are not pushing the same buttons as were pushed for the Wolverine film (FBI involvement?) or they don’t really care about it. I’d like to think it’s the later, and that gives me hope that that some people in these big organisations have a clue about the internet and online piracy.

The “theatrical box office” takings of a movie are much higher than DVD sales, so it makes sense for the distributors (20th Century Fox in this case) to go bat-shit-crazy when something happens that affects their primary income. The distributor will also get a much higher % of the box office than it will from DVD sales (it costs a lot more to produce and sell a DVD than licensing a print to a cinema) and whilst the box-office is 5x the DVD gross, it might be 20x the revenue they actually receive.

The way my provider has very quickly removed the content shows that copyright violations *can* be enforced if there is enough will (although I’m not too sure of the state of the P2P systems) but at the same time suggests that run-of-the-mill violations like TV episodes or DVDs are allowed to exist.

Sabbatical

Saturday, April 4th, 2009

It’s been 18 months since I left my day job, and I’m happy to say it’s been a pretty good 18 months. In the same time period prior to going solo I went through 3 jobs and lived in 3 different cities. Some say that makes me a little flakey and that it reflects negatively on my “CV”; but to me it shows that I can identify when I’m not enjoying life and that I’m prepared to do something about it. I’m sure we’ve all met people who bitch and moan about their jobs but they never quit, or address the reasons why they’re unhappy. That’s definitely not me ;-).

I’ve reached another one of those moments where I feel like I have to change some things up. It’s a combination of work and personal reasons but right now I feel like I’m just bobbing along; I don’t have any big goals I’m working towards and whilst I’m not unhappy, I’m not particularly happy. I need some time to reflect on life and so I’ll be taking a sabbatical over the next few months.

I’m looking forward to it a lot. I’m planning on spend my programming-cycles finishing off the first release of APRIL and working on some other projects that always came 2nd best to “real work” (I need to put phpevents.com to good use). My eyes are also on some geeky conferences coming up. I’ve booked for Bamboo Juice and can’t wait to spend a week relaxing in Cornwall followed by a fix of geekiness. I’ll also certainly be jumping back on the train and making the next few PHPLondons. Generally it’s going to be some time where I can get some things done without the pressure/distraction of work.

I need to sort out my long term plans too. I’m tired of telling people the same old spiel about Pluggable.

Thanks for asking, things are going great! I’ve got loads of work on at the moment - more than I can manage. I’ve been thinking of expanding, perhaps taking on an employee or bringing in some other freelancers - perhaps a CO-OP thing? Not sure…

Everyone who has asked me “how’s business?” in the last year got that response. I need to shit or get off the pot, either I’m building a business or I’m a freelancer (either is fine, so long as I know wtf I’m doing!).

I’ve never been great at the long term. Ask me what I’m doing in 5 years and I don’t have an answer, the best I’ve got is a vague picture of where I’d like to be by 30 (+3 years). I am however starting to grasp that time does indeed march on, and that it’s worth doing the things that seem small and insignificant in the short term, but which have great value in the long term (and that are only achievable after a long period of time). Sucks, I feel like a grown-up!

I love you really Apache

Saturday, April 4th, 2009

I kinda slagged off Apache’s roll in a lot of web applications. The gist of that post is that Apache’s traditional life-cycle has been replaced by functionality within the web app itself (eg the web app handles authentication, rather than the web server). What I totally missed though, and it seems patently obvious now, is that Apache’s ability to be used as an HTTP wrapper for PHP apps is a good thing.

If you take away the logging/resolving/auth/negotiation/etc stages you’re left with a daemon that speaks HTTP. At this “stripped away” stage it receives an HTTP request and passes it through to PHP, which does some processing and then Apache returns an HTTP response. That might not seem like much functionality but it’s an absolutely critical part of a web app. My last post suggested that using Apache in this way was “bloated overkill”:-

We don’t really use Apache for much, so why do we use it at all? I think it boils down to being a nice host environment for PHP. There’s nothing really wrong with that, except PHP is tightly coupled to Apache. I think Apache rocks, but if we just end up using it as a host for PHP, then that’s bloated overkill? What about if we stripped out the features of Apache that we dont use until we’ve got a lightweight http wrapper for our PHP app to run in… isn’t that what Rubyists do with Mongrel?

I’m an idiot. Apache’s ability to be used as an HTTP wrapper isn’t a bad thing: it’s a good thing!

If you view Apache like this then you’ve got a rock solid foundation to create an “HTTP application server” (ie a standalone daemon communicating via HTTP). It’s one of those pieces of software that you can run on pretty much any environment and has been QA’ed over years. It’s solid, trusted and “fast enough”.

I remember talking to Rob Allen about potentially moving to Ruby or Python and one of the barriers for him was reliability. With PHP we can stick scripts in Apache’s docroot and forget about them - they’d probably survive a nuclear winter. Neither of us had the confidence that you could do the same with a Ruby/Pyhon stack (eg running Mongrel or CherryPy).

PHP is like a scripting language for Apache - and that’s a massive strength.

Going back to the old school

Friday, April 3rd, 2009

PHP stands for “PHP: Hypertext Preprocessor”.

In computer science, a preprocessor is a program that processes its input data to produce output that is used as input to another program

Hypertext in this context means HTML. The idea was you write PHP code which is then sent to the preprocessor to execute and it sends you back HTML. Instead of having a static HTML file on your website you would have a PHP file which would be interpreted and converted into HTML. It was common for every page on a site to have its own PHP file. It made sense: users were migrating from static HTML pages which had their own files, so they would replace them with a PHP file.

Copy & Paste code duplication quickly became a problem. Code would be refactored into functions and stored in a .inc file, to be included by the .php script.

I remember a lot of my .php files looked like this:-

<?php
include("inc/common_funcs.inc");

if($_POST['submit']){
  // form was submitted, do some logic
}
?>

<html>
  … webpage + form goes here (and the form’s action would be the same page as this
</html>

There were a few other styles I remember too. One was to have a php file for the webpage/form and another for the processing of the form (ie the form’s action); this was a throw-back from the Perl CGI days where the static HTML would reference the .cgi file. Another was to pass a “cmd” or “action” variable and then have a switch statement to control the application flow.

<?php
  if($_POST['submit']){
    switch($_POST['cmd']){
      case “save”:
        // do the logic for “save”
    }
  }
?>

If you showed code like that to any PHP developer today they’d probably rip you a new one and say it’s crappy old school. But that last example should ring some bells - it’s pretty much the precursor to today’s frameworks’ Controller/Action style and fundamentally is the same thing (and probably runs faster). Sometimes I think that not a lot has changed in the PHP world from ten years ago :-).

I’ve been thinking about frameworks for a while. For me I’ve never really found one that clicks with how I work, or how I grew up learning PHP. I like things really simple - like loading a .php file in the browser and my code being executed. No routing, no dispatching, no jumping through a dozen files before my code runs. When I use frameworks the magic stuff scares and frustrates me; sometimes I feel almost claustrophobic - I’m stuck inside its way of doing things and I can’t get out.

A couple of years ago I designed a framework for a company who needed their “old school” code refactored. At the end of the project I remember turning to a colleague and exclaiming that I thought “it totally frigging rocks!”. What (I thought) it did very well was to make it easy for their internal developers to write new pages for their site. It took care of some of the setup stuff (bootstrap, loading config data, db layer) and then got out of their way.

<?php
class SomeHandler{
  public function run($context){
    $db = $context->getDb();
    $request = $context->getRequest();

    // do some logic

    $person = $db->getPerson("Richard");

    $context->setViewVar("person", $person);
    $context->setTemplate('person.tpl');
}
?>

The developer writes their logic inside the run() method and has access to stuff like the DB or Request through the $context object. Their job is to write some logic and then set some variables for the view, that’s it. To give you some context, this class is instantiated by the “bootstrap” index.php file, based on some trivial routing, something like this:-

<?php
//bootstrap index.php

// setup context the context
$context = new Context();
$context->setDb($dbObject); // etc

$handler = "SomeHandler"; // replace this with some simple routing

$handler->run($context);

if($context->template){
  $templateEngine->render($context->template, $context->viewVariables);
}
?>

As I remember it, URLS mapped 1:1: to handler files. The routing component was a little involved because it tied into another system, but for prototyping we used a router that directly translated the path to a class. So “/path/to/foo” would be the handler “Handler_Path_To_Foo”. We’re back to writing one .php file to handle one url, back to the old school. The templating system was there to use if they wanted (by setting a template in the $context object) but if not then no worries - whatever they echo’ed from within run() would be output.

I can’t claim this is amazingly complicated or innovative, but I think that’s kinda my point: it was dirt simple but it was really effective. The learning curve was practically zero for new developers, the system just augmented the basic offering of PHP (and made some “company specific” tools available to them).

I’ve got an archive of the project and today I counted the number of lines for the “framework” and it came in at about 600, including comments and empty lines which is tiny, almost nothing. But looking back on the code I think it’s some of the best code I’ve ever designed/written.

Why blog this now? As a part of APRIL I’ve been looking at the “server” component recently. This is the part of the library that helps a developer create their own RESTful webservice. In essence that means it’s “A Framework”: it handles the routing/mvc part of things (”Oh no, not another framework!”). I’m using the same sort of design for this as I did for the framework I talked about. It’s ultra-lightweight, I’m a bit embaressed to even call it a framework, but from the work I’ve done on it so far I’m getting the same feeling as I did before - it’s simple, fast and “feels” like old school PHP, in a good way.