Elephpants

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! 😀

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!