Category Archives: Software Development

Software Development

Recursively remove/delete a directory in PHP using SPL components

File system management is not the most common use case for PHP, but in writing a command line tool today I was surprised to find that PHP doesn’t have a function to recursively remove a directory (I was expecting at least a flag for rmdir or unlick, but no, nothing).

I did a quick Google search, just to be sure, and found numerous other people asking this same question and a few rather long winded recursive functional solutions floating around.

No being much of functional programming (and also because really there is no excuse for it when PHP has had reasonable OOP and some fantastic stuff in the SPL for a long time now) I knocked together a much cleaner SPL based solution for recursively deleting a directory:

function recursiveRmDir($dir)
{
    $iterator = new RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST);
    foreach ($iterator as $filename => $fileInfo) {
        if ($fileInfo->isDir()) {
            rmdir($filename);
        } else {
            unlink($filename);
        }
    }
}

I’ve obviously not added any checks to ensure that rmdir and unlink are successful, but this would be a simple addition and I really wanted to post this as an example of a nice modern way to use PHP rather than relying on old functional components.

Share

New features in PHP 5.4

The next iteration of 5.4 looks like it’s more focused on cleaning up the language than features.

It appears many of the previously deprecated functions are now, as promised, removed completely. As this article is about features, here is a quick summary of the other changes:

  • Removed: break/continue $var syntax
  • Removed: register_globals, allow_call_time_pass_reference, and register_long_arrays ini options
  • Removed: session_is_registered(), session_registered(), and session_unregister()

This information is based on alpha/beta releases and is subject to change.

Onwards to the new features…

Traits support

PHP 5.4 includes traits which allow reusable groups of methods to be used from any class.

Here is an example:

trait someMethods
{

    public function doSomething() {}

    public function doSomethingElse() {}

}

We can then use the above trait in an class using the use statement:

class someClass extends someParent
{

    use someMethods;

    public function doSomethingLocaly() {}

}

More information about traits can be found in the manual:


Array dereferencing

As PHP has had object dereferencing for ages, this is something that is really long overdue and addresses the inconsistency between object and array return values.

A quick/common example of the existing object dereferencing:

class someClass
{

    private $_someVar;
    private $_someOtherVar;

    public function setSomeVar($value)
    {
        $this->_someVar = $value;
        return $this;
    }

    public function setSomeOtherVar($value)
    {
        $this->_someOtherVar = $value;
        return $this;
    }

}

$class = new someClass();
$class->setSomeVar(1)->setSomeOtherVar(2);

Now we can do the same with arrays, without the current need to assign the array return value to a variable before we can access it’s contents:

class someOtherClass
{

    public function getArray()
    {
        return array(1, 2, 3, 4, 5);
    }

}

$class = new someOtherClass();
$third = $class->getArray()[2];

This is a rather pointless example, but demonstrates the new functionality.

Share

Installing sqlite on a rooted Android phone.

Many rooting tools install the sqlite3 binary by default, but the pre-rooted rom on my SGS seems to be an exception, so here is a guide to install it manually.

You will need the SDK tools on your computer for this to work!

Connect the phone, bring up a shell and get root permissions:

$ adb shell
/ $ su
#

Find the device for the system partition:

# mount                                
rootfs / rootfs ro,relatime 0 0
proc /proc proc rw,relatime 0 0
sys /sys sysfs rw,relatime 0 0
/dev/block/stl11 /cache ext4 rw,noatime,barrier=0,data=writeback,noauto_da_alloc 0 0
/dev/block/stl10 /dbdata ext4 rw,noatime,barrier=0,nodelalloc,data=ordered,noauto_da_alloc 0 0
/dev/block/mmcblk0p2 /data ext4 rw,noatime,barrier=1,data=ordered,noauto_da_alloc 0 0
tmpfs /dev tmpfs rw,relatime,mode=755 0 0
devpts /dev/pts devpts rw,relatime,mode=600 0 0
none /acct cgroup rw,relatime,cpuacct 0 0
/dev/block/stl6 /mnt/.lfs j4fs rw,relatime 0 0
tmpfs /mnt/asec tmpfs rw,relatime,mode=755,gid=1000 0 0
none /dev/cpuctl cgroup rw,relatime,cpu 0 0
/dev/block/stl9 /system rfs ro,relatime,vfat,log_off,check=no,gid/uid/rwx,iocharset=utf8 0 0
/dev/block/stl3 /efs rfs rw,nosuid,nodev,relatime,vfat,llw,check=no,gid/uid/rwx,iocharset=utf8 0 0
/dev/block/vold/179:1 /mnt/sdcard vfat rw,dirsync,nosuid,nodev,noexec,noatime,nodiratime,uid=1000,gid=1015,fmask=0002,dmask=0002,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
/dev/block/vold/179:9 /mnt/sdcard/external_sd vfat rw,dirsync,nosuid,nodev,noexec,noatime,nodiratime,uid=1000,gid=1015,fmask=0002,dmask=0002,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
/dev/block/vold/179:9 /mnt/secure/asec vfat rw,dirsync,nosuid,nodev,noexec,noatime,nodiratime,uid=1000,gid=1015,fmask=0002,dmask=0002,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
tmpfs /mnt/sdcard/external_sd/.android_secure tmpfs ro,relatime,size=0k,mode=000 0 0

The important bit here is /dev/block/stl9 /system rfs which is needed for the next step.

Remount the file system rw (replace rfs and /dev/block/stl9 with the values from the output of mount):

# mount -o remount,rw -t rfs /dev/block/stl9 /system

Next, in a separate terminal copy the sqlite3 binary to the phone (can be obtained from the Super One Click package http://www.shortfuse.org/):

$ adb push sqlite3 /sdcard/
42 KB/s (24120 bytes in 0.557s)

Back in the original terminal, move the binary to /system/bin and set permissions:

# cp /sdcard/sqlite3 /system/bin
# chmod 4755 /system/bin/sqlite3

Finally, remount the /system filesystem read only again:

# mount -o remount,ro -t rfs /dev/block/stl9 /system
Share

The most awesomest design pattern, ever to exist and that ever will exist!

At my current day job, someone came up with this design pattern and has implemented it in almost every class throughout the entire application, there are probably close to 100 implementations of this “pattern”. I had to post it because it’s just so awesome!

Firstly let’s late a look at the traditional factory method, it’s usually a static method that instantiates 1 or more objects and returns you the object.

class MyClass
{

    public static function factory($some, $params)
    {
        $it = create_some_object($some, $params);
        return $it;
    }

}

Simple and effective.

Behold! The tri-class factory method, 3 times more awesome than the normal factory method!

Firstly create your class as normal:

class MyClass
{
    ...code...
}

Then create a factory class, (you’re thinking, “what???”, but don’t think, just do):

class MyClassFactory
{
    public static $factory = null;
    ...static methods...
}

Note, some very important features, you must have the public static $factory variable, and it must be initialised to null, despite this being the default behaviour of PHP. The methods also follow a very important structure too, but we’ll get to this in a moment.

Finally, the 3rd and final class, the “Factory Impl” class:

class MyClassFactoryImpl
{
    ...non-static methods...
}

Then to put everything together we need to assign an instance of “Factory Impl” to the static $factory variable in the Factory class, this should be down outside of all the object structures, for added coolness:

MyClassFactory::$factory = new MyClassFactoryImpl;

Confused? You should be. But this is just the start of the awesomeness.

Next, you need to implement the factory methods, they should be implemented as normal public methods in the “Factory Impl” class, e.g.

public function getById($id)
{
    return new object($id);
}

For each method you create, you also need a static function to call this method in the factory class:

public static function getById($id)
{
    return self::$factory->getById($id)
}

Repeat for all your factory methods, and you’re done!

I would recommend that to make it as helpful as possible for other developers, make sure you put everything in one file and try and call the file something different from any of the class names you’ve used, this will ensure that they will probably have to use a require_once each time they use it as it’ll confuse most autoloaders.

You should end up with something like this:

class MyClass
{

    public function __construct($id = null) {}

    public function someMethod() {}

    public function someOtherMethod() {}

}

class MyClassFactoryImpl
{

    public function getEmpty()
    {
        return new MyClass;
    }

    public function getById($id)
    {
        return new MyClass($id);
    }

}

class MyClassFactory
{

    public static $factory = null;
   
    public static function getEmpty()
    {
        return self::$factory->getEmpty();
    }

    public static function getById($id)
    {
        return self::$factory->getById($id);
    }

}

MyClassFactory::$factory = new MyClassFactoryImpl;

There is no denying the awesomeness of this pattern, 3 times more objects, 3 times more memory, 3 times bigger stack, 10 times more code, god knows how many times more inefficient, but infinitely more fun! :D

Next week, stay tuned for the “super god” class!

Share

Web server benchmark PHP – Apache vs Nginx vs Lighttpd

This is a quick benchmark of the 3 major *nix web servers, to see which gives the best performance.

Test system:

Hardware: vps247 cloud VPS node, 512Mb ram.
Software: Ubuntu 10.10 RC (kernel 2.6.35-22), Apache 2.2.16 (mod_php), Nginx 0.7.67 (php-fpm), Lighttpd 1.4.26 (php-cgi), PHP 5.3.3 with Xcache.
Site: LiteMVC 0.1 Hello World.

Test process:

Using ab with 10,000 requests, 10 concurrent requests.
Monitoring load/memory usage with htop.
All web servers with default configuration, 1 site enabled.
All tests were repeated 3 times.

Apache 2.2.16:

Test 1 Test 2 Test 3
Load at start: 0.03
Memory usage at start: 130 Mb
Load average max: 15.88
Memory usage max: 290 Mb
Requests per second: 400.04
Time per request: 2.5 ms
Longest request: 806 ms
Load at start: 0.07
Memory usage at start: 130 Mb
Load average max: 21.73
Memory usage max: 332 Mb
Requests per second: 395.49
Time per request: 2.529 ms
Longest request: 728 ms
Load at start: 0.06
Memory usage at start: 121 Mb
Load average max: 20.95
Memory usage max: 253 Mb
Requests per second: 385.82
Time per request: 2.592 ms
Longest request: 937 ms

Nginx 0.7.67:

Test 1 Test 2 Test 3
Load at start: 0.05
Memory usage at start: 129 Mb
Load average max: 0.77
Memory usage max: 157 Mb
Requests per second: 413.40
Time per request: 2.419 ms
Longest request: 239 ms
Load at start: 0.05
Memory usage at start: 152 Mb
Load average max: 0.93
Memory usage max: 169 Mb
Requests per second: 409.15
Time per request: 2.444 ms
Longest request: 232 ms
Load at start: 0.00
Memory usage at start: 163 Mb
Load average max: 0.61
Memory usage max: 169 Mb
Requests per second: 415.28
Time per request: 2.408 ms
Longest request: 250 ms

Lighttpd 1.4.26:

Test 1 Test 2 Test 3
Load at start: 0.00
Memory usage at start: 115 Mb
Load average max: 0.81
Memory usage max: 139 Mb
Requests per second: 427.92
Time per request: 2.337 ms
Longest request: 120 ms
Load at start: 0.09
Memory usage at start: 136 Mb
Load average max: 0.73
Memory usage max: 143 Mb
Requests per second: 447.10
Time per request: 2.237 ms
Longest request: 111 ms
Load at start: 0.03
Memory usage at start: 140 Mb
Load average max: 0.69
Memory usage max: 159 Mb
Requests per second: 454.13
Time per request: 2.202 ms
Longest request: 178 ms

Conclusions:

Lighttpd was the fastest overall in this test, however what probably stands out the most is the inefficiency of Apache with both server load and memory usage increasing considerably during the test. The reason for this may be down to Apache spawning additional processes when it is receiving a large volume of requests, which results in the higher memory usage. The memory usage with Nginx and Lighttpd increases by a much smaller amount because each of them runs a fixed number of PHP CGI processes (20 in this case).

Apache is definitely the winner in terms of module support and ease of configuration, but I may have a somewhat biased view as I’ve worked with Apache for years. Nginx is probably the hardest to setup and configure, however after switching from PHP-CGI to PHP-FPM this made things easier and it does seam there are more configuration examples floating around on the net than for Lighttpd. Lighttpd is notorious for memory leaks, I’m not sure if this is still an issue with current versions.

Share

First Android app completed.

My first Android app has now been completed and is available on the Android Marketplace, it’s not going to be any use to anyone who is not using the vps247 cloud service, but for anyone is it’s a very handy app.

Here is quick overview of the features available in the app:

1. Resource viewer, displays current memory, disk, IP and bandwidth usage:



2. Server viewer, displaying all servers and a traffic light to indicate the server status, servers can be tapped to show detailed information and access start/stop controls:



3. IP address viewer:



4. Create new servers, which has a nice interface with sliders and drop downs to make it easy to configure a new server. The available resources are used to dynamically generate the creation interface:



To download the app, just search for ‘vps247′ in the Android Marketplace and it should show up!

Share

Today is a confusing day for 32 bit PHP

As many people probable know, there are some glaring inconsistencies between 32 bit and 64 bit PHP, especially when it comes to large numbers and floats.

For some reason, best known to the PHP developers, 32 bit PHP is limited to the maximum signed 32 bit integer size of 2147483647, while 64 bit PHP uses the 64 bit equivalent of 9223372036854775807. Many languages overcome the 32 bit / 64 bit differences by storing 64 bit ints as 2 32 bit ints to allow the usage of 64 bit integers on 32 bit systems, hence providing complete consistency between the two platforms.

These inconsistencies similarly apply to floating point precession, presumably a float in 32 bit PHP is equivalent to a 32 bit float in C, whereas it appears that in 64 bit PHP it’s actually a double.

Here is a nice example of the inconsistency, today’s unix timestamp in float form:

32 bit:

php > echo (float) 1285200000;
1.2852E+9

64 bit:

php > echo (float) 1285200000;
1285200000

This is an important thing to remember with PHP, despite the fact it is a loosely typed language, they behaviour is equivalent to the C behaviour for the particular architecture of your server.

Share

First attempt at a (useful) Android app

I’ve had the Android SDK installed for a few months, but down to lack of ideas and time I’ve not really done anything with it.

One of the hosting companies (VPS247) I am using at the moment have just released an iPhone app, as well as an API, they’re exposing almost the full functionality of their cloud hosting admin area via the API which is quite a nice idea. With lack the lack of an Android app I decided to have a go.

So far I’ve created a ‘login’ which just requires the API key of your account:

Once you’ve entered a valid 40 character API key, the API is then queried twice for your account resource info and details of virtual servers that are currently setup (I’ve blurred my host names as I don’t think anyone really needs to know these):

Here is the package file if anyone want’s to give it a try: VPS247Monitor

Share

First Go Library

My first library written in the Go programming language, GoMySQL, is now complete and available to download from my projects pages.

The library fully implements the MySQL protocol and supports the majority of the common features found in other libraries, including:

  • Single queries
  • Multiple queries
  • Change database
  • Prepared statements

There are a number of other libraries available for Go, but none of them (as yet) implement the full protocol.

To download visit the GoMySQL project page.

Share