phpWomen contest close to an end

Tomorrow will be the last day to participate in the phpWomen contest at www.phpwomen.org.
For those of you who do not know, phpWomen is a great initiative to bind the girls of the trade together in a sort of community or usergroup.
It has received quite a bit of attention lately and in my opinion rightly so!

I have participated in the contest and am actually a bit ashamed that I have only blogged this late about it. I have been busy writing my own entries and reading that of others. I hope that authors will continue to write pieces on the forum. I know I will be there and talk as long as there is something to talk about!

phpWomen does allow men to post on their forum, so you do not have an excuse to pass by and say hello and even add some of your knowledge to the whole.

Kubuntu and a slow Eclipse

You know you want to do it, the move, from windows to Linux when developing locally. It works like a charm, except for Eclipse when you do not pay attention.

One could wonder, why do you want to develop locally? Well, sometimes (twice a day) I travel by train. And sitting still is very hard for me (tend to get bored easily) and thus I wanted to be able to do some work in that time. When you do not have a 24×7 UMTS high speed internet connection (or alike) you just have to do it sometimes.

This is where Linux plays an important part, in linux you can simulate your production environment as closely as possible to make deployments and or exchanges as painless as possible.
Unfortunately Eclipse tends to grind to a halt when you want to do anything more complicated then typing.
Fortunately, there is a fix!

First of all, install Eclipse (I advise the downloadable version with PDT included because the Aptitude version of Eclipse is one number behind (3.2) and PDT 1.x won’t run on it (hasn’t got WST 2.0.0 or higher).
When done, get the Java Sun 6 package from Aptitude with the following command:
sudo apt-get install sun-java6-bin sun-java6-jdk sun-java6-jre
Did it? Good!

Unfortunately Linux might not understand you actually want this to be your default JVM so you need to edit /etc/jvm and type the path where your JRE is installed at the top of the list (usually this is /usr/lib/jvm/java-6-sun
sudo nano /etc/jvm
In some case Eclipse does not dig it either (annoying, is it not?) and you can force Eclipse as well to use your new Sun JVM by placing the path in the /etc/eclipse/java_home file (or wherever your eclipse installation path is)
sudo nano /etc/eclipse/java_home
Last but not least: tell your linux version to use this JVM with the following command:
sudo update-java-alternatives --set java-6-sun
After this, your eclipse (and other Java applications) will work like never before!!

Errorhandling in PHP, now with Fatal Errors included

Every once in a while you stumble upon a topic that you thought would have been completely covered. This I thought as well about PHP Errorhandling. Although resources are abundant about the topic, most of them cover Error handling without handling the Fatal Errors or with hacks for a solution.

This last problem can easily be solved as I wil explain in this post. We wil build an errorhandling class which starts trapping errors from the moment it comes into existance.

What comes first in such a task is to review what it must do, for this specific class we assume the following:

  • We run PHP 5.2.0 or higher, this is needed for our little trick
  • The class must be a singleton
  • It must trap all possible errors, including fatal errors
  • Once trapped, we can do custom handling with them and then pass the error to the defaulthandler

The class skeleton

Since we have stated as a requirement that our class should be a singleton, we will have to define the constructor as being private and create a getInstance method. The latter will instantiate the class the first time it is called.

Below is an example of how to do a singleton class skeleton:

1
2
3
4
5
6
7
8
9
10
11
class My_ErrorHandler {
  private function __construct() {}
 
  public static function getInstance() {
    static $Instance = null;
    if ($Instance === null) {
      $Instance = new My_ErrorHandler();
    }
    return $Instance;
  }
}

Although this post is not about Singletons in PHP I might as well explain a little about the code above.
In our new class we define the constructor as private, which effectively means that we can only instantiate this class from within and not from another class.

Protected would do as well and would perhaps be better because classes which extend this one can also call the constructor, but I leave this open to debate

And to round up we create a static method named getInstance (sounds logical?) which contains a static variable holding the instance. When this method is called we check whether the static variable contains our instance and if it does not, we instantiate is.
It really is that simple.

For the sake of completeness I must add that another way is to define our instance variable as a private static class variable instead of a method variable, this has the added advantage that other functions in the class could use it. I personally like to encapsulate it this way.

Errorhandling as you know it

Now that we have the basis for our errorhandling class, let’s create an error handling method as described in the PHP Manual. It is actually quite simple and I will quickly go over it.

All one has to do is define a method and register it to PHP with the set_error_handler call. A method needs to have 5 parameters:

  • An error level, this gives an integer corresponding to the E_ error constants fo PHP, i.e. E_WARNING
  • A message, here we receive the text accompanying the error
  • A file name, this is the file in which the error was triggered
  • A line number, this is the line number where the error occurred
  • An errorcontext, this is an array containing all the variables and their values in the scope where and when the error occurred

Now we can choose, do we fully handle the error ourselves, including handling when to die the script or not?
This can be done by the return value of our method. If we return false, the default PHP error handler handles the error. And if you do not specify a return value PHP will believe you handled the complete error and continue executing where it was.
I usually return false since I want to catch the errors and do something extra, like send it to the Syslog or sending an e-mail in some cases.

Example:

1
2
3
4
5
6
public function error($level, $message, $file, $line, $ctx){
  if (error_reporting() > 0) {
    // Do something here ...
  }
  return false;
}

You might have noticed that I check whether error_reporting is larger than zero. This is because of the at(@) operator. If we do not check this we end up handling the errors which were supposed to be silenced via the @ command.

To wrap this part up, we insert the set_error_handler call to the constructor of our class. This makes sure that once the class has been instantiated, we immediately start catching errors.
By the way, if you intend to send output to for example a Syslog, make sure you tell PHP to give plain text message instead of HTML typed.
Should you intend to use assert statements, add ASSERT_WARNING to your assert options.

Another example showing all of the above:

1
2
3
4
5
private function __construct() {
  ini_set('html_errors', '0');
  set_error_handler(array(&$this, 'error'));
  assert_options (ASSERT_WARNING, 0);
}

Catching the fatal error

The above is all fun but well, it still does not catch the dreaded fatal error which let’s your application end up all white.
Well, this is what we are going to address now and is actually really simple once you know it (and you run PHP 5.2+, but you do that, right?)

With PHP 5.2.0 a new command was introduced, namely: error_get_last.
This function returns the last error which has occurred in PHP. Although it might not sound exciting, try combining it with register_shutdown_function.

If you register a shutdown function which calls error_get_last and checks if an error has occurred, it can call our previously defined error function with the given level, message, file and line. With this call we have automatically caught our fatal errors as well!

Let me show you:

1
2
3
4
5
6
7
function shutdown() {
  $error = error_get_last();
  if ($error != null) {
    $this->error($error['type'], $error['message'], $error['file'], $error['line'], 0);
    exit;
  }
}

And we call the register function in our constructor as well:

1
2
3
4
private function __construct() {
// .. Things we have defined above ..
  register_shutdown_function(array(&$this, 'shutdown'));
}

Conclusion

This is all there is to it, if you call My_Errorhandler::getInstance() at the beginning of your script then all your custom error handling will automatically be invoked.
You can use the power of this function to pro-actively handle errors instead of waiting for users to report them to you. Just couple this with your Syslog or auto-mail the development department and you will know when things go bad.

I know I had fun writing this, I hope you have by implementing this!

Testing LDAP

Since I tend to work quite a bit at home, for the company or just for myself, I need to work off the Business Intranet quite some times.

This causes some problems when you are developing certain functionalities like a LDAP authentication mechanism in my new SOA Platform (I will blog about it in a later stadium). One of the problems is a lack of a LDAP server (how surprising).
Usually this should not cause any problems since most types of servers are found online and have portable versions suitable for Windows, the OS I am currently stuck with.

However, LDAP is a different story. OpenLDAP, the most used implementation appears to be linux only and the windows port approximates ancient. Thus we had to look onwards for an alternative. After one and a half hours searching I found the Apache Directory Server (http://directory.apache.org/apacheds/1.5/index.html)
, which seemed a blessing at that time.
Currently I am toying with it and seeing what it can do, it looks like a good product but the learning curve is a bit steep since no GUIs are installed with the product. This has dampened my enthusiasm somewhat but I have just downloaded Apache DS Studio (based on Eclipse) and will see how it all works out..

In my next blog item more about this!

Dutch PHP Business Seminar == excellent

I like to learn, it might seem odd for some people but I just like it. There is never enough time to learn everything but attending a Seminar is surely one way to increase your learning efficiency.

And so I did attend the Dutch PHP Business Seminar for the first time. It was offered by iBuildings and Sogeti and covered SOA and Security in PHP. Both are topics in which cannot get enough attention!
Two tracks were available, Engineering and Management. In my role as developer and ‘representative’ of my company I choose to attend the Engineering track.

Opening

First off, Ivo Jansch (CTO iBuildings, www.jansch.nl) opened the Seminar with a speech about the penetration of PHP into the Enterprise business’. Although technically it did not give insight into any subjects, I very much enjoyed the statistical and historical information which was given.
For example: Did you know that of the top 10 visited sites in the Netherlands, 7 are running on PHP? And that 45% of the dutch domains use PHP to display their pages?For me as trivia fanatic I love to hear about those facts, I believe I even bored some friends and collegues with those afterward.

Webservices in PHP

After Ivo finished his talk it was time to start the Engineering Track. Here we were welcomed by Peter Verhage. Who gave an overview of Web services and which technologies are being used. Basically he named REST, SOAP and JSON as ways to accomplish interaction between the services and showed some source code. Although it usually is a matter of choice which way to go, every technology has it’s advantages. I do not intend to repeat his speech here, so i will just post the sheets from slideshare.

PHP Secure Application Development

Robert van der Linde gave this rather impressive speech. What he told sounded like it should be common sense but you could not get the feeling ‘Am I doing enough about it?’. This rather distinct and at times uncomfortable feeling was amplified by the strong points given by Robert.
I could feel myself consciously implementing whitelisting on services, two days after this speech when I was working on a Webservices platform. Thus I can only conclude that it had made an impact on me.

Last but not least: the keynote

The keynote was given by Cal Evans, a PHP Evangelist and Editor at Zend (if you know PHP you should know him). As last year his talk was amusing, insightful and at times disturbing (in a good way). He gave a speech about the changing role of the IT Specialist, that we change from ‘Gatekeepers’ to ‘Gardeners’. Although I would like to rephrase the words as changing from ‘Secret Society Member’ to ‘Caretaker’. And I could not shed the feeling that I had to agree that we need to be open to our users and give them a voice.
I think Cal told it best and should you wish to reread his comments about the seminar you can so do on his blog.

Conclusion

At first my conclusion would be that this blog posting has become far too big, but I just had to write down my experiences of the Seminar and with them my thoughts and feelings about the talks which have been held.

All in all, I had a blast!

My first talk

I knew it when the hype begun.. I tried to postpone what I knew would be inevitable.. Alas, I could not resist or stay behind anymore.. I needed.. a blog

From the moment blogging became popular, I thought that what I had to say was not important enough. At least not in my opinion. Time has gone by and I have come to see that I might have something valuable to say or at least to write. Though I could be mistaken, it is becoming wise to at least save my findings for my own benefit or of those around me.

My main passions in life are Development and Tabletop RPGs, one can expect most blog postings to be about this. Although I have the flaw of losing interest in posting on a blog or forum after some time I do intend to keep up this blog since it is also for my own sake.

Should you agree or disagree with anything I say, just leave a comment (no trolling please, this will be ignored and perhaps even removed). It will be read.

| Log in

  Copyright 2008, Mike van Riel