Finally namespaces

There’s a lot of whining whining about the new namespace separator these days. Not only on blogs. At first I was a bit negative towards the choice but is more and more convinced it is a good one.

First of all, few of the people complaining seem to have read the RFC documents explaining in detail the pros and cons of different choices. So here goes, read carefully before deciding.

To be honest. If some of the other suggestions in the RFC “Namespace separators” above would have won I think a lot more people would have had better reasons to rant. The backspace “\” is probably the most readable choice and thus less prone to typos which is a Good ThingTM.

Most of the criticism seem to stem from the problems you could get from having namespaces in strings and that backslash is used for escaping. I am a unix based developer but I guess that to developers using windows this is a no brainer. They’re already used to have a backslash in another context. If you then have a look at the way many projects, including for example Zend Framework, organize their classes and what should be namespaces into directories it makes sense to a lot of people.

So it’s not all bad. I’m looking forward to not having to instantiate insanely named classes like

$upload = new Zend_File_Transfer_Adapter_Http();
PHP

Two weeks with Zend Studio for Eclipse

After more than ten years with Emacs and terminal flipping as my primary development environment, whether for C, PHP, WSDL, HTML/CSS or javascript, I decided to try (I mean really really try) an IDE for a while. As PHP is my main focus these days I have been looking towards Zend Studio for Eclipse. I figured Eclipse with it’s maturity must work well enough on linux.

Why am I not happy with emacs and the power of the command line and all the tools available a few key strokes away you might ask? Well, as being a lazy programmer is nowadays considered being a plus, I want some more help from the environment. I want to be reminded about all those FIXME’s and TODO’s scattered in the code. I want better integration with subversion, better and easier testing and reliable refactoring support for starters.

Getting started

It is easy try an IDE and quickly loose interest. You start the program, look around, get annoyed when you don’t find the keyboard shortcuts or don’t understand the concepts. I’ve done that before. I know this and decided to use Zend Studio on the biggest project I’m on at the moment. The team members are located in two different countries, use different operating systems and time is sparse. Everything needs to run smoothly. Blissfully ignoring the tingling bells warning me not to do it I imported the project into Zend Studio from subversion.

That part went really well. It seems easier to import a project from subversion than directly from a directory structure.

Using the IDE

One of the concepts I always have had trouble understanding is when the IDE keeps weird project files in the code tree. Thouse files should not be part of the project. Team members should be free to use whatever tools they find best for their productivity. Zend Studio does not do this even though it has it’s own status for files which is good. It can still be a bit annoying when Zend Studio gives me the weird error Could not open the editor: Editor could not be initialized when opening files. Huh? If you look at the java exception, available as error details, it only means the resource or file have been changed from outside of Zend Studio. To open it you need to refresh the file through F5. A minor nuisance but why not tell me the file is changed and ask if I want to reload the file? It can’t be that hard.

There are many good things though. I like having PHP Documentor just a click away. Using the refactoring feature and being able to reliably rename class methods throughout the project is a big plus. Code folding, inline language warnings and errors in the editor and easily navigating between different places in the code are also things that makes life easier. It is also something you’d expect in any modern IDE. Sometimes I miss grep on the command line but it is probably just a matter of getting used to the GUI way of searching.

So all in all I’m happy this far. The IDE gives me the support I want. But there are a few things that I think is problematic.

Subversion integration. It is easy to see the history of a file and compare against different revisions directly in the IDE. But it is actually very hard to see the small annotation icons which indicates if a file has local changes. (I have a 17″ laptop with a screen resolution of 1920×1200.) And the other day when committing a change that spanned many files in many directories not all of the changed files were selected for commit. I didn’t notice this and subsequently the when updating the test environment it broke. Easily fixed but still annoying. This problem together with the editor not handling file updates from outside of the IDE is making life a little bit harder than it should be.

Encoding support. I am in the habit of using UTF-8 for everything. This particular project was originally created on a windows machine and all the files were encoded in ISO-8859-1. I didn’t give it much thought as i’ve been on the project for some time and Emacs have always detected the encoding without bothering me. Zend Studio for Eclipse use UTF-8 as default but completely ignores the encoding in the already existing files. I didn’t notice the problem until a team member using windows called me and asked why I screwed up the files.

It is my responsibility but the stupid editor didn’t exactly help me. Setting the project encoding to ISO-8859-1 and then manually fixing the faulty files removed this issue.

Both of the above problems can be fixed by me being a bit more careful now that I know more about the IDE. It is still serious when you can’t trust the IDE fully.

The last and most annoying bit is the speed. Or rather lack of speed. The program is slow. Dead slow. I have a modern dual core laptop with 3 Gb RAM running Ubuntu 8.04.1. Zend Studio for Eclipse is a bit slow to start with. After working a few hours in the IDE the editor can’t even keep up with my writing. After a long day the laptop sometimes start swapping. I don’t know if it Eclipse, the Zend Studio part or java itself. The speed is still not acceptable.

Summary

I chose to try the Professional edition as I want, among other things, subversion and refactoring support which is not available in the community version. The price is 399€ which isn’t that much if it increase my productivity enough. The slow editor and the fact that I have to double check many things isn’t really a productivity boost though. I haven’t used all the features I thought I needed. On the other hand I have only tried it with an old, non Zend Framework project. Support for the framework, which I use for new development, is integrated into the IDE in various ways and I haven’t had the time to try it out.

The trial version is working another two weeks. Before then I have to decide if the extra features in the commercial version are worth 399€.

PHP

Slowly coming back to life

Well, I have been gone from blogging for quite a while now. Much have happened since the last post. Most notably is that I quit my job to focus on my web development company Mesh. This resulted in a huge increase in the workload for a while. Things are settling and work is now more and more a normal every day task though I still have plenty of things to do. Both in Sweden and Spain.

I am finally converting my old blog away from my own, very old, solution into using wordpress. Unfortunately I have lost all comments due to some holes in the old solution.

As I work on PHP related projects (more than) fulltime now I will again focus on writing about things of interest in the PHP sphere.

My new office

This is our office at Mesh

PHP, Personal

Setting up a development environment with apache-2.2.6

I recently bought a new laptop as my new development box. A nice 17″ screen with 1920*1200. So now I was to setup a PHP development environment for all the projects I’m working on. I downloaded Apache 2.2.6, PHP 5.2.4 and MySQL 5.0.45. I compiled and installed them and finally added a few virtual hosts. None of the vhosts worked. They all gave me

"client denied by server configuration: ..."

I had missed that apache now ships with a slightly different configuration for the / directory to

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
</Directory>

The Order deny,allow and Deny from all makes the virtual hosts inaccessible by default. When I googled it I saw many forum and blog posts asking about this issue.

To fix it you can either remove/change the lines. But that is probably not a good idea. Better is to explicitly specify which directories that apache are allowed to serve. So for me and my project setup it was as simple as adding a new directory directive opening up my www directories for all the projects.

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
</Directory>

<Directory "/home/myusername/Projects/*/www">
    Order deny,allow
    Allow from all
</Directory>

I hope this short explanation helps a few people looking for a solution to the problem.

PHP

International PHP conference in Frankfurt 2007

Yesterday was the first day of the International PHP Conference. I listened to Lars Jankowfsky in his session “Practising Agile Development”. Although different methodologies were mentioned it was mainly a one day session on how Extreme programming works. The session had a good mix of fun och seriousness. One of the reasons I wanted to go to Frankfurt this year was the mix between technical and non technical sessions. I have to say that Lars one day session on XP was a very good start. As always me and others that do not use xdebug and unit tests frequently got a lot of well deserved bashing…

Using your brain while developing is really helpful” – Lars Jankowfsky. Maybe I should stop the bad habit of lazy coding while watching TV…

One thing that makes me happy is that they have added a session on Zend Framework. I look forward to that as I have begun to rely on Zend Framework quite a lot.

IPC 2007 and Ajax In Action

IPC 2007 and Ajax In Action

Lars Jankowfsky during his session

Lars Jankowfsky during his session

Javascript, PHP

Reordering nested sets using PHP and Javascript

The adjancency method

Representing a tree of nodes, for example product categories, in a relational database is not hard. The most common way of doing it is to store a parentid with each node. This is also known as the adjacency list model. It is sometimes also referred to as the recursive model. Whatever the name it is easy to grasp. It is very simple to insert and move nodes around but it is not an optimal solution if you often select the whole tree from the database for displaying on a web page as you need to recursively select all children of nodes.

The nested set model

A much better solution is the nested set model. In this model trees are represented as nested sets. Instead of using a parentid you have a left and right value for each node expressing the relationship to other nodes. A tree of nodes can look like this.

        34
         |
         +--36
         |   |
         |   +--46
         |   |
         |   +--47
         |
         +--38
             |
            +--39

The same data can be viewed more graphically as nested sets. This is also the way they are stored in the database. Notice how the left and right values are numbered in order from the outermost left to the outermost right.

                            id:34
   +----------------------------------------------------+
   |             id:36                     id:38        |
   |   +------------------------+    +--------------+   |
   |   |    id:46      id:47    |    |     id:39    |   |
   |   |   +-----+    +-----+   |    |   +------+   |   |
  1|  2|  3|     |4  5|     |6  |7  8|  9|      |10 |11 |12
   |   |   +-----+    +-----+   |    |   +------+   |   |
   |   +------------------------+    +--------------+   |
   +----------------------------------------------------+

In the database this is stored as in the following table.

 +----+-----+-----+
 | id | lft | rgt |
 +----+-----+-----+
 | 34 | 1   | 12  |
 | 36 | 2   |  7  |
 | 46 | 3   |  4  |
 | 47 | 5   |  6  |
 | 38 | 8   | 11  |
 | 39 | 9   | 10  |
 +----+-----+-----+

This allow you to select the full tree with only one select statement. Normally you select the full list of nodes including the depth of each node as shown below in the small table containing id and depth. It is then easy to loop over the data in PHP to produce a nice tree in HTML/CSS.

The data below can be displayed as the the tree on right. The depth is in parenthesis.

 +----+-------+        34 (0)
 | id | depth |         |
 +----+-------+         +--36 (1)
 | 34 | 0     |         |   | 
 | 36 | 1     |         |   +--46 (2)
 | 46 | 2     |         |   |
 | 47 | 2     |         |   +--47 (2)
 | 38 | 1     |         |
 | 39 | 2     |         +--38 (1)
 +----+-------+             |
                            +--39 (2)

Here is a more thorough article on how you use MySQL with nested sets.

On to the problem

Last night I was working on an application that display a full tree where the user must be able to to drag and drop nodes to reorder the tree. The problem is that it is much more difficult to insert or reorder the tree using the nested set model.

For the actual html I produced nested ordered lists using the OL and LI html element so that it was easy to use with the excellent MooTree script based on MooTools. (It does seem to have trouble with the latest version of MooTools though.) After reordering the structure it was easy to extract the tree list including the depth of each node. I sent this to the PHP backend to be handled there. The text data I sent to the PHP backend then look like below.

 34,0
 36,1
 46,2
 47,2
 38,1
 39,2

As you can see it looks exactly like the data I selected from the database previously. So I had to create a function in PHP that took this indata and produced a list of nodes with the correct left and right values. This list is then looped over to update all the nodes in the database.

function depth2nestedset($data)
{
  $lines = explode("\n", $data);
  $rows = array();
  $stack = array();

  $lft = 0;   // Left value
  $rgt = 0;   // Right value
  $plvl = -1; // Previous node level

  foreach($lines as $line) {
    list($id, $lvl) = explode(',', $line);

    // Skip empty/faulty lines
    if (trim($id) == '') {
      continue();
    }

    if ($lvl > $plvl) {
      $lft++;
      $rgt = 0;
      array_push($stack, $id);
    }
    else if ($lvl == $plvl) {
      $pid = array_pop($stack);
      $rows[$pid][2] = $rows[$pid][1] + 1;
      $lft = $lft + 2;
      $rgt = 0;
      array_push($stack, $id);
    }
    else {
      $lft = $lft + ($plvl - $lvl) + 2;

      $diff = $plvl - $lvl + 1;
      for($n = 0; $n < $diff; $n++) {
        $pid = array_pop($stack);
        $rows[$pid][2] = $lft - $diff + $n;
      }
      array_push($stack, $id);
    }

    $rows[$id] = array($id, $lft, $rgt);
    $plvl = $lvl;
  }

  $plvl++;
  $cnt = count($rows) * 2;
  $leftovers = count($stack);

  for($n = 0; $n < $leftovers; $n++) {
    $pid = array_pop($stack);
    $rows[$pid][2] = $cnt - $plvl-- + $n;
  }

  return $rows;
}

The resulting associated array that is returned looks like below.

Array
(
    [34] => Array
        (
            [0] => 34
            [1] => 1
            [2] => 12
        )
    [36] => Array
        (
            [0] => 36
            [1] => 2
            [2] => 7
        )
    [46] => Array
        (
            [0] => 46
            [1] => 3
            [2] => 4
        )
    [47] => Array
        (
            [0] => 47
            [1] => 5
            [2] => 6
        )
    [38] => Array
        (
            [0] => 38
            [1] => 8
            [2] => 11
        )
    [39] => Array
        (
            [0] => 39
            [1] => 9
            [2] => 10
        )
)

That makes the circle complete. From left/right values in the database to a node tree based on depths that is easy to manipulate in html and then back to left/right values. Hopefully this makes sense. Do you have a better solution or more examples on applications that do this I would love to hear about it.

Javascript, PHP

PHP Conference

I have been to the International PHP Conference in Frankfurt a couple of times. Last year I didn’t go as the quality of the conference 2005 wasn’t what I think one should expect.

This year seems to be different though. A whole slew of interesting sessions on PHP mixed with a few sessions on agile development. Sounds just about perfect to me. I am especially interested in the one PHP 6 session as I have not had the time to follow what is happening in regards to next major version of PHP. I’m definitely going to Frankfurt this November. There are still too many sessions in German if you ask me – but I guess there are quite a few Germans going to the conference.

PHP

Generating Excel files with PHP

I’m on this project at work where we need to create an excel file and send it via email or ftp to a recipient. The normal answer to this is to create a comma separated file and name it .xls and excel will work it out. However, that only works if you open it directly through an http request and are able to set the http headers correctly. I’m not going to elaborate on the requirements but they want an excel file generated and sent to them. They don’t want to and can’t download the file themselves.

Naturally I went to the almighty Google for the answer. There are a few excel writers written out there. I found a tutorial on using PEAR Spreadsheet_Excel_Writer and an MS-Excel Stream Handler class to mention two solutions.

I found one piece of code floating around numerous places that I fell for. I am not sure about the original source for this code though. Extremely simple.

function xlsBOF() {
    return pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
}

function xlsEOF() {
    return pack("ss", 0x0A, 0x00);
}

function xlsWriteNumber($Row, $Col, $Value) {
    return pack("sssss", 0x203, 14, $Row, $Col, 0x0)
      .pack("d", $Value);
}

function xlsWriteLabel($Row, $Col, $Value ) {
    $L = strlen($Value);
    return pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L)
      .$Value;
}

I liked it because it did what I wanted with a minimum of fuss. I was happy until I realized I don’t understand it enough to be able to set the character set. Right now question marks appear in the texts instead of national Swedish characters as å, ä and ö.

PHP

SVN hosting options

At work we still use CVS. We even have PVCS sitting on an old Tru64 machine actively used in one last project that hasn’t converted to CVS. I have looked at subversion/SVN and I use it for a few smaller projects. I host the repositories myself which is not the right option in every situation. And for some projects it would sometimes be better to host them somewhere else. This is especially true for many open source projects. Yesterday I found a good subversion hosting overview over at Snook.ca.

However, before I decided to host a software project somewhere else I would thoroughly investigate how easy it is to export the whole repository with version history intact.

PHP

PostNuke and PHP 5

In an earlier blog post I wrote about how I think companies, and open source projects, will loose business if they don’t start the upgrade path to PHP 5. With PHP 6 in the works it is only a matter of time before most people will consider PHP 4 applications legacy. Now that there is a clear message that no new versions of PHP 4 will be released after December 31st, 2007 this will go much faster.

In the long run I think that it is not enough to just be PHP 5 friendly while staying backwards compatible.

There is so much added benefit in using PHP 5 and probably PHP 6 later on that projects not taking advantage of new features will eventually be left behind. New developers will more often than not start with PHP 5 when learning the language. I think many open source projects will have difficulties in attracting new developers if they don’t actively promote using new features in the language. Some projects will most likely fork. A fork is not always a good solution with all the problems and politics that usually comes with it. It would be stupid not to prepare for new PHP versions in advance and avoid all the problems.

The other day I had a conversation with an industry collegue from back home working on a poker site using the PostNuke CMS. He is not very technical but still has many itches to scratch. Most of the issues he aired was mostly due to a lack of specific functionality he needed though. He was forced to create many pages, for example a listing of poker sites, manually. This he admitted might just be due to the fact that PostNuke is the wrong system for what he is doing. We had a very interesting discussion regarding PHP CMS applications and functionality.

But after our discussion and after reading “The state of the PHP5 CMS…” by Lukas Smith I thought it would be interesting to have a closer look at PostNuke, being a well known and widely used PHP application. If I searched google for “drop support for php 4″ I get about 11 million hits. I guess a lot of the hits aren’t really relevant but still. If I add the word “PostNuke” to the search I can’t find one relevant hit in the search result. So what is a project as PostNuke doing, if anything, with PHP 5?

PHP