Dominique Stender A blog about my thoughts and experiences in Information Technology

15Feb/100

Thoughts on HipHop for PHP

It has been almost two weeks now since Facebook announced HipHop for PHP. The source code is still not available because apparently they've ran into some compilation issues, according to their GitHub status. So in essence, not much is known about HipHop for PHP but I want to try to give my thoughts based on the small amount of information we have and my experience with similar technologies.

Please note that the bits and pieces of information come from various sources and things may or may not be exactly as we think they are.

In order to understand what HipHop for PHP does it is important to understand how PHP itself gets executed. In this article I will give a rough overview of the PHP execution process and make a few educated guesses in what aspect HipHop for PHP differs from there.

Based on the little information available online I'll discuss how HipHop for PHP is suitable for large businesses and what might turn out to be obstacles that are yet to remove.

29Dec/090

Another reason to upgrade to PHP 5.3

php codeIn case you haven't found any specific reason to switch to PHP 5.3, I have one: Static properties.

Consider the following scenario: You have a group of classes, each responsible for returning a specific statistic. All these classes implement the same API, as given by a common abstract class.

Now, some of these statistics are publicly accessible, others require a certain access control permission to be set.

The logical approach would be to implement a static property to the abstract class, denying access by default. Those classes that are indeed public will overwrite the static property with the correct setting and become accessible.

The property is static to gain speed: There is no need to instantiate the class, if the access control is not satisfied.

Stripped down to the bare essentials the whole set of classes to outline the issue looks like this:

abstract class StatisticBase {
    public static $acl = '0';
} // end: class StatisticBase

class PublicStatistic extends StatisticBase {
    public static $acl = '1';
} // end: class StatisticBase

class AdminStatistic extends StatisticBase {
    // default value for $acl is fine
} // end: class AdminStatistic

AdminStatistic is an implemented class that is private, indicated by the $acl property being 0. PublicStatistic on the other hand is public, hence $acl is 1.

Now before instantiating a specific class we can check whether or not the access condition is satisfied:

if ($user->isAdmin == true || PublicStatistic::$acl == 1) {
    // retrieve admin statistic...
} // end: if

But naturally this is cumbersome as soon as you have more than a handful of statistics - you don't want to add another condition to your if-statement whenever you add a statistic. So the logical solution is a loop:

$statistics = array(
    'PublicStatistic',
    'AdminStatistic'
);

foreach ($statistics as $statClassName) {

    if ($user->acl == 1 || $statClassName::$acl == 0) {
        // retrieve current statistic...
    } // end: if
} // end: foreach

Here PHP 5.2 will fail with a fatal error.

You can't access static class properties by means of using a variable for the classname. PHP 5.3 can. There is no really elegant solution for this. (If you know one, post it in the comments!)

While I'm glad that PHP 5.3 has one more OOP feature covered, It leaves me with the feeling that we still have a long way to go until PHP fully supports object orientation.

Now, if Novel would please release a PHP 5.3 package for SLES 10 and SLES 11... thank you

3Dec/090

Optimize your Wordpress Blog Performance

snowflakeThe famous blogging software Wordpress has always been my tool of choice when setting up a blog-like website. The usability of the backend and the number of available themes and plugins make it ideal if you want to see quick results.

However, when it comes to SEO and bandwidth optimization and standard compliance many of the plugins fall short and require some additional attention. The good news is that usually many plugins are available to scratch a single itch. Picking the right one often avoids many glitches.

Social network plugins and social bookmarks

I want to reach out to people with this blog. This includes enabling my honored readers to quickly share my articles with their community through the various social networks.

TweetMeMe Button and Facebook Share are two plugins that put nifty buttons in your blog that include counters to boast about how many people shared your content. Both are really great and work perfectly, but I encountered some issues due to which I removed both from this website after a test period of a couple of days.

I had to remove the TweetMeMe Button simply because it loads content from the tweetmeme.com server which is currently very slow, thus slowing down my website as well. There is nothing the author of this plugin can do about it I guess.

Facebook Share on the other hand was removed because it uses an id="" attribute for the button it displays, even if the button is on the page multiple times. Since this is violating W3C compliance this plugin had to go, too.

In addition to that, both plugins cause my layout to look a little bit messy and 'cheap', which I try to avoid. Again, that is nothing that can be fixed by the plugin authors since it is just my personal opinion. I really liked both plugins but these minor glitches made me move on to...

Tweet This - CSS sprites

I found another nice extension for social bookmarks called Tweet This. The author Richard X. Thripp did a really great job on this, offering bookmark icons for not one but many social networks. That covers my bases.

However I enhanced this plugin slightly in order to utilize CSS sprites for the social bookmark icons. My friend Chris recently talked about CSS sprites on his blog SwiftLizard.com. Here is the concept in a nutshell:

If you display ten icons on your website the traditional way the browser has to request ten files from the server - one for each icon. Since usually browsers do only a handful of requests at a time this slows down your website. With CSS sprites however, you can utilize CSS and one single graphic containing all icons to achieve the same effect with only one image being requested. This results in website response times that are much lower, thus enabling a smoother surfing experience.

Further optimization of Social bookmarks [added]

Since I posted this article I further enhanced the way I handle social bookmarks in this blog, you might want to check that out.

WP Super Cache

The plugin WP Super Cache almost needs no introduction. Written by Donncha O. Caoimh it enables us to circumvent PHP completely and serve static content in many cases. Naturally, this is much faster. The plugin is a must for anyone taking performance seriously. WP Super Cache alone reduced the initial response of my blog (time-to-first-byte) by 80%.

iPhone and mobile browsers

As the owner of a technology related website I believe it is reasonable (and my access statistics seem to agree) to believe that some of my visitors will visit the website from a mobile device such as the iPhone or a Blackberry. While technically the layout I chose for this blog looks ok in my iPhone, there is a lot of clutter that I don't want to wait for on my mobile device.

Thankfully there is a plugin called WPtouch which displays the blog in a highly stripped down but yet good looking manner when viewed with a recognized mobile browser. Not only do mobile visitors get their content faster by removing clutter, the usability is also highly increased. Perfect.

Combining JavaScript and CSS files

Similar to what I said earlier about many images slowing down a website, the same is true for many JavaScript or CSS files. Regardless of the file type you should keep your number of requests as low as possible. With Wordpress and its many plugins that is next to impossible though, unless you want to rewrite every plugin you install.

But there is hope. What CSS sprites achieve for icons, the Head Cleaner plugin does for JavaScript and CSS files. It can combine all JavaScript files into one and do the same for CSS. Further, JavaScript can be minimized and both file types can be compressed, reducing not the number of requests but the bandwidth as well.

Image size

This is a classic. Double check the images you have on your website for size - the size measured in bytes, not pixels. If you're not sure how to reduce images byte-wise, there are online services like the one on dynamic drive which can guide you (and save you lots of manual trial-and-error time).

General rule of thumb is that a png will be smaller than a gif and images with only a few colors should never be stored in a jpg. For example the ScrumMaster badge you see on the upper right was initially a jpg of 25kB. My all means the png I use now looks identical and is only 13kB.

Results

Given all the measures above, I was able to save over 50% of bandwidth and 75% of requests on the frontpage of my blog. No matter how fast your broadband connection is, you feel the difference. Even better, the website is still W3C compliant.

When in doubt what else you can do, I can recommend registering for Google Webmaster Tools and install their Page Speed Firefox plugin. It will hook into the famous Firebug plugin and provide you with an additional tab that gives very useful hints how to improve your website speed.