rickogden.com

PHP 5.4 – The Highlights

ElephpantsI haven’t blogged for ages, so here’s one about the impending release (ie. today) of PHP 5.4! I am just giving a brief introduction to a few features which I think make it a rather interesting release.

Speed

There is going to be a significant speed increase in PHP 5.4 (apparently more so than we got from 5.2-5.3). According to http://news.php.net/php.internals/57760 there is a 20 – 50% increase from 5.3!

Arrays

There are a couple of new features in arrays. The first of all is array creation. You can now create an array using the JavaScript style square-brackets syntax [], rather than having to call the array function.

//in php 5.3 or below
$firstArray = array();
$secondArray = array('foo', 'bar');
$thirdArray = array('foo' => 'fooval', 'bar' => 'barval');

//in php 5.4
$firstArray = [];
$secondArray = ['foo', 'bar'];
$thirdArray = ['foo' => 'fooval', 'bar' => 'barval'];

This will please us JavaScript developers. Also what will please us is the new array dereferencing ability. You’ve long been able to reference an array element from a returned array in JavaScript, but in PHP you’ve had to assign it to a variable and reference from the variable:

//in php 5.3 or below
$arrayVar = $myObj->returnArray();
echo $arrayVar['foo'];

//in php 5.4
echo $myObj->returnArray()['foo'];

So these give us programmers more ways to be lazy! :D

Class Member Access on Instantiation

Also like JavaScript, PHP 5.4 allows you to call/access a method/property immediately after instantiating the object (without assigning it to a variable).

//in php 5.3 or below
$foo = new Foo();
echo $foo->bar();

//in php 5.4
echo (new Foo)->bar();

Be aware that this instantiates an object, calls the bar() method, and then throws the object away. Generally this is pretty wasteful and often a static method is a more efficient way of doing it. However, it is nice to have this feature.

Traits

And another way we can be lazy is we can use traits. Traits are described as “compiler assisted copy and paste”. In a trait you can include create methods and properties which can be reused in multiple classes. This is not the same as inheritance, which means that you can use multiple traits in a single class. A great example of a use of a trait is for a singleton class (whether or not you agree that singletons should be used at all) which I saw at the PHPUK Conference.

trait Singleton {
  protected static $_instance;

  public static function getInstance() {

    if(self::$_instance == null) {
      self::$_instance = new self();
    }

    return self::$_instance;
  }
}

//now our singleton class, let's call it DatabaseHandler

class DatabaseHandler {
  use Singleton;

  private function __construct() {
    //...
  }
  //...
}

//So now you can do:

$dbh = DatabaseHandler::getInstance();

So, as stated before it is a compiler assisted copy and paste, that means a method in a trait can use private methods in the object, and vice versa. You can use as many traits as you like in a single class (unlike inheritance), and you can even change the visibility and create aliases for trait properties and methods on a per class basis. This is incredibly flexible and allows for increased reuse of code.

Built in Web Server

Finally, PHP 5.4 comes with its very own web server, allowing you to start developing without having a server stack. It is started by executing the PHP CLI binary with the -S switch followed by server:port (eg. localhost:80). Simple as that! It uses your current working directory as the root directory and router can even be specified.

devmachine:project user$ php -S localhost:80

Note: Do not use this in a production environment! Ever!

More Information

I for one am very excited about these features, and really looking forward to teaching and using them in production. As this is a brief overview, I have only highlighted certain new features, for a more in-depth analysis:

  1. Davey Shafik – PHP 5.4: The New Bits, this is the talk that Davey gave at PHPUK2012 (recommended read)
  2. A far more complete overview on King Foo
  3. An overview article on webtutor.pl of PHP 5.4

When Objects Act Like Arrays

I’m a big advocate of object-oriented programming, even though only a few years ago I hated it and thought it was “over engineering”. I don’t believe, however, that object orientation is suitable for every circumstance. Anyway, that’s for another blog post. Objects are great, but not always the best when handling data, particularly things like data collections.

One of the nice things about storing collections of data in arrays rather than objects is the fact that you’re able to easily loop through the collection, and extract data from the collection depending on the position. Sometimes though we may want to manipulate the collection, and store more information than just the data in the collection. This is where SPL comes in.

As of version 5.0 PHP has included the SPL (Standard PHP Library). I am going to look at a couple of interfaces from this library in this blog post: ArrayAccess and SeekableIterator. These interfaces allow us to do a couple of things: SeekableIterator allows us to iterate through the object (eg. a foreach() loop), and the ArrayAccess allows us to extract data from the object as if it was an array. All this in a way that is completely controlled by the programmer.

Definition for the SeekableIterator:

SeekableIterator extends Iterator {
/* Methods */
abstract public void seek ( int $position )

/* Inherited methods */
abstract public mixed Iterator::current ( void )
abstract public scalar Iterator::key ( void )
abstract public void Iterator::next ( void )
abstract public void Iterator::rewind ( void )
abstract public boolean Iterator::valid ( void )
}

Definition for the ArrayAccess:

ArrayAccess {
/* Methods */
abstract public boolean offsetExists ( mixed $offset )
abstract public mixed offsetGet ( mixed $offset )
abstract public void offsetSet ( mixed $offset , mixed $value )
abstract public void offsetUnset ( mixed $offset )
}

The way these work is that they require a variety of methods to appear in the class. With SeekableIterator there is generally a “position” property which is just an integer holding the current position in the collection (so normally defaults to 0). The methods that are required are: seek, current, key, next, rewind and valid. The “seek” moves the position to the number passed in as an argument, “next” method normally increments the position by one, and “rewind” resets the position back to 0. “current” returns the item at which the position property is at, “key” tells you the current value of position and “valid” checks to make sure an item exists at that position. You can then put the object in a foreach loop as if it was an array!

foreach($iterableObject as $item {
     ...
}

ArrayAccess requires fewer methods, and some of them are similar: “offsetExists” is the same as “valid” but rather than getting the position from the internal position property, it is passed in as a parameter. “offsetGet” returns the item at a given position and “offsetSet” sets an item to a given value. You can then treat an object like an array by using standard array notation:

$item = $arrayAccessObject[0];

A single class can implement both of these interfaces without conflict, and SPL contains many more things like this. I have not written this as a tutorial as the PHP Docs are incredibly good on this subject and easy to follow.

http://php.net/manual/en/class.seekableiterator.php
http://php.net/manual/en/class.arrayaccess.php

Installation of Memcache in PHP on Ubuntu/Debian

Happy new year all! Just a quick post about how to install Memcache in PHP on an Ubuntu or Debian server (I think it will apply to Redhat based servers, just substitute the apt-get for yum).

Log on to your server as root (either directly or using sudo su), then install the Memcache daemon through apt:

apt-get install memcached

Once installed you need to install the Memcache module into PHP. This is done through PECL which in turn is installed through PEAR. When they are both installed, you need to run the command:

pecl install memcache

This will install the memcache extension, and once done will tell you to add the the line “extension=memcache.so” into your php.ini file. I prefer keeping these in separate files (so that the php.ini file can be updated). The easy way to do this is:

echo "extension=memcache.so" > /etc/php5/conf.d/memcache.ini

And finally, reload Apache

/etc/init.d/apache2 reload

If successful, Memcache will now appear in your phpinfo().

5 Cool Things in HTML 5 for Mobile Web App Development

Following on from my previous HTML 5 article, I decided to write another one talking about a few cool things that HTML 5 brings us which help us develop cross-platform web applications. Although not all devices support HTML 5 at the moment, it is increasingly becoming the standard due to its relatively low resource consumption and accessibility.

Canvas

Canvas is pretty much what it says on the tin, it is an element which can be used by JavaScript draw draw graphics on-the-fly. These graphics can range from static graphics (such as graphs), to animations all the way through to fully rendered 3D images using WebGL. The idea of canvas is that it’s a blank canvas for you to work with.

https://developer.mozilla.org/en/Drawing_Graphics_with_Canvas

Audio/Video

HTML 5 introduces the audio and video elements. These elements are used to embed audio and video content into your website. As these are raw media files (unlike Flash which includes its own player), it relies on the web browser to be able to decode and control the content. Standard still has not been agreed upon, with some browser developers wanting to use Ogg (the open standard), and others wanting to use h.264. This currently makes it difficult for developers, so while we’re waiting for a clear victor, it’s probably best that we include both!

https://developer.mozilla.org/En/Using_audio_and_video_in_Firefox

Offline Caching

The problem with web applications is that they require a continuous internet connection. This can become even more problematic when using mobile devices, as there may be no guarantee of coverage (particularly if the user is on the move). HTML 5 solves this problem by using offline caching. Using manifesto files you can tell a browser what files it should cache, what files require an internet connection (ie. shouldn’t be cached) and a fallback if content is not available. This can also be used to reduce the need to communicate back to the server continually, thus saving you, and the user precious bandwidth.

http://www.webreference.com/authoring/languages/html/HTML5-Application-Caching/

Client-side Storage

We are used to cookies being used to store data on the client, but they are rather inflexible. This is sorted with the use of client-side storage. There are a few different types of this: Session storage will store data associated with a particular instance of a website. What this means is that the data is persistent as you navigate through a website, but if you open up another instance of the website (such as in another tab or window), then the session is different. This means you can have different data stored with different instances.

Local storage is more like cookies, however unlike cookies the data is not sent to the server in every HTTP request, it is only retrieved as and when needed. This means that data isn’t unnecessarily sent to the server allowing you to store more data and save bandwidth. The data persists when the window is closed and (unlike session storage) can be accessed across all tabs/windows. The data is associated with the domain.

If you are wanting to store more complex data and be able to search through it easily, you’re able to use a database store. This uses the SQLite spec, and means you’re able to query the database file and retrieve only relevant information to be processed or sent back to the server.

http://www.webreference.com/authoring/languages/html/HTML5-Client-Side/

Geolocation

One of the big things about Smartphones and mobile applications is that they can determine the location of the user, and therefore bring up information relevant to the location. This was done differently by each device and required direct access to the OS API. HTML 5 has standardised the methodology to retrieve the location, and does it by whatever means the device can handle (eg. Wi-Fi networks if the device has a wireless card but no GPS). As this is implemented by the browser, there is no need to (as developers) worry about how the location is retrieved.

http://diveintohtml5.org/geolocation.html

HTML 5 Preparation

When setting up an HTML 5 web page, there are a couple of things which need to be done. First of all you need to standardise the CSS for the new HTML 5 elements. This can be done at the top of your CSS stylesheet by making all the new elements be displayed as “block” elements:

article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section,summary{
  display: block;
}

Unfortunately there is still a range of browsers which still don’t render HTML 5 objects. This means we need to add a JavaScript hack. Rather than downloading or creating your own JavaScript file, you may as well just link to a file that has already been created for you on Google Code. This means that it will always be kept up to date. Also as it is only needed for IE browsers up to version 8, you won’t need other browsers to load it:

<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

You are then free to start creating your HTML 5 web page!

PHPNW10 – An Overview

I had a fantastic time at phpnw10 this year. Met loads of old friends, and made a few new ones. The main conference kicked off on Saturday morning with a keynote by Lorna Mitchell entitled “Teach a man to fish”. This was about how team training is important with proper feedback mechanisms which enable a developer (and a team) to learn from their work and give them the skills to teach themselves further. Following that I went to see Derick Rethans giving a talk on geolocation within PHP. This gave me a great insight into how geolocation/mapping works online and has given me some food for thought for my MSc. After, I headed back to the main track to see Ian Barber speaking about debugging. I find that this is the sort of talk I can’t get enough of, and discovered a couple of cool new tools to help with debugging on top of the existing ones I already use.

After lunch I saw Michelangelo Van Dam talking about unit testing within ZF. Like everything with ZF, it seemed a lot easier than I thought it might be. Then there was another ZF talk in the same room by Rowan Merewood who, as always, gave a rather entertaining talk. It was about Zend_Acl and used real-world examples (ship classes from Star Trek) to demonstrate how they work.

Scott MacVicar gave a really interesting talk on HipHop for PHP, allowing PHP code to be compiled. He went through the process of using it, what the current limitations are and the plans for the future. As this was Scott, heckling was aplenty, and made this talk very enjoyable! Then it was back to the main track for the final session: The Framework Shootout, a panel session chaired by Marcus Deglos. Although there were no definitive conclusions that came out from the panel, it was very interesting to see different people’s take on things, and the strengths and weaknesses of a variety of frameworks.

Overall it was an excellent event with excellent attendance! However we have a bit of a dilemma now. We have reached the capacity of this venue. This means that if we want to expand we will need to find a new one. If you have any thoughts/ideas/recommendations on not only venues around Manchester, but how we can expand further to increase the impact of the PHPNW group, please let me know! Finally I would like to thank Jeremy Coates and all the helpers on the day, the event ran fantastically smoothly and it was all down to you, thanks everyone!

Why Students Should Attend PHPNW10

PHPNW is primarily a conference aimed at professionals within the industry to allow them to learn from each other and discover new ideas and techniques which they can then apply to their every day work. What is less prominent is how this is equally as useful for students, who may want to work in this same area once they have graduated.

Read the rest of this entry »

Virtual Hosts for Development with Apache on Ubuntu

I do a lot of development on Ubuntu, as I often have multiple projects on the go which are nothing to do with each other, it’s often easier to create separate virtual hosts on my local development machine. This means that when they are ready for the “real world”, they are already set up as isolated sites at the root of their domain (rather than in a subdirectory of an existing site).

In order to do this, you need to create a new virtual host in your Apache config. Create a new file in the directory /etc/apache2/sites-available and open it in your favourite editor. It doesn’t matter what the file is called, but it’s best to keep it descriptive. We’ll call this project “mysite”, so the file can be called “mysite”. In the file we need to configure the Apache virtual host.

<VirtualHost 127.0.0.1>
ServerName mysite.localhost
DocumentRoot /var/www/mysite/public/
</VirtualHost>

In the VirtualHost tag, you put the IP, seeing as I only want this for local loopback (for development) I have just put 127.0.0.1. The ServerName is the URL that you use to connect to the site and the DocumentRoot is where the public documents are stored. This is a very basic set up, so there are many more options you can add.

To make the site enabled, you create a symbolic link to the file from the sites-enabled directory.

cd /etc/apache2/sites-enabled
ln -s ../sites-available/mysite mysite

You now need to add the subdomain (mysite.localhost) to the list of hosts, so open /etc/hosts in your favourite editor and append the line:

127.0.0.1 mysite.localhost

And then restart Apache:

sudo /etc/init.d/apache2 restart

Now you should be able to visit http://mysite.localhost on the local machine (assuming the directory does actually exist).

This should also be similar on MacOS and other linux Distros, but the file locations (particularly for Apache) will vary.

LTSP Part 2 – Configuration

In the previous post I had the problem:

… when I boot up, I get the Ubuntu boot screen, which shows it’s connecting to the terminal server, however it then fails with an errror saying

Error: Failed to connect to NBD server

And I get sent to a basic busybox shell.

There were two reasons for this.

PXE… booted

First of all, because I obtain the DHCP separately from the network boot, I need to treat it as if it’s a static IP. LTSP can handle static IPs, but this posed a couple of problems. I would need to specify a separate config file for each MAC address in the pxelinux.cfg/ directory. Secondly it would require each MAC address to be given the same IP each time (this was not going to happen).

So instead of getting gPXE to PXE boot, I completely bypass pxelinux.0 and use my own boot script. In this script I pass in the IP and other information. gPXE has some environment variables which can be used for this, so I wrote a script (The ‘x’s should be replaced by the terminal server’s IP address).

#!gpxe
dhcp net0
kernel tftp://xxx.xxx.xxx.xxx/ltsp/i386/vmlinuz ip=${ip}:xxx.xxx.xxx.xxx:${gateway}:${netmask}:${hostname}:eth0:none nbdroot=xxx.xxx.xxx.xxx:2000
initrd tftp://xxx.xxx.xxx.xxx/ltsp/i386/initrd.img
boot vmlinuz

This script retrieves the kernel, and passes as parameters the environment variables (which were set by the dhcp) the IP, gateway, netmask and hostname. Another parameter is the nbd server location and port. The we retrieve the initial ramdisk (initrd) and boot.

More haste less speed

After that was fixed, on my test machines it still didn’t connect to the NBD server. This is because my test machines are core2duo 3ghz with 4gb RAM; they were so fast at booting up, that it didn’t get a response from the NBD server in time. I diagnosed this by adding the parameter:

break=mount

To the kernel line in the script above. This stopped the boot, then it got a response from the server, and when I pressed ctrl+D (to continue the boot) it booted up fine. This is a bug in the ltsp_nbd script.

I solved this by logging into the terminal server and opening the file /opt/ltsp/i386/usr/share/initramfs-tools/scripts/ltsp_nbd, then added the line:

sleep 5

after the line:

ip link set lo up

This meant that the script paused for 5 seconds to allow the NBD server to respond.

Once edited the initramfs needs updating, as do the kernels:

chroot /opt/ltsp/i386 update-initramfs -u
ltsp-update-kernels

I will be writing a part 3 to this sometime soon talking about some of the customisations I will be adding.