PHP 5.4: What you need to know

5.4 is here (or 5.4.3 at the time of writing) and with it comes new language features, notably traits and a built-in web server, performance and memory usage improvements and removal of some legacy features that were deprecated in 5.3.

Whats New?

Traits [docs]

These could be described as reusable class components with the aim of introducing a form of multiple-inheritance into a single-inheritance language. So rather than a possibly complex inheritance chain, the generic components can be extracted into traits.

As an example lets take the singleton pattern and apply it using a trait. [codepad]

trait Singleton
{
  public static function Instance()
  {
    static $instance = false;
    if ($instance == false)
      $instance = new self();
	return $instance;
  }
}
class Blogomatic
{
  use Singleton;

  private function __construct()
  {}
  public function __toString() { return 'BLOG!'; }
}
$b = Blogomatic::Instance();
echo $b; //emits BLOG!

Short Array Syntax

We can now define array without using array() just like javascript.

//Determined by roll of a dice
$randomNumbers = [5,3,6,2];

Member Access on Class instantiation

We can now use a class without having to assign it to a variable, this also has the plus side that the instance is destroyed after you have finished using it (example on codepad)

In Short: (new Apple())->CompareTo(new Orange())

Both this and the short array syntax bring more flexibility and cut out some of the verbosity inherent in php.

Built-in Webserver [docs]

Competing for first place on the feature list (along side traits) this brings straight forward local development of php applications, so no more having to install a (w/l)amp stack just for development. All you would need is a database server.

This does require you to write some code to route requests as there is no support for .htaccess files.

There is also the argument that the environment does not reflect the production or test environments and so might cause odd bugs and incompatibilities because of those differences, but I say that as long as you are not relying on specific server modules and are being cross-platform in the way you write php (ie using DIRECTORY_SEPARATOR instead of /) then the benefits outweigh the potential drawbacks.

New Functions

There are more, but here are my highlights.

hex2bin($hexstring) [docs] This is the missing half to bin2hex($bindata).

http_response_code($code)[docs] This is a nice alternative to header('HTTP/1.1 404 Not Found'). It will also return the current response code.

 

Whats Old

Call time pass by reference

You can no longer call a function or method while giving an explicit reference.
ie computeRasberryPi(&$randomNumbers).
Instead the function you are calling will need to define the parameter as a reference.
ie function computeRaspberryPi(array &$numbers).

Safe Mode, Magic Quotes, Register Globals

Yes, they are gone, they will not be missed.

Safe mode was a flawed attempt to enforce security in a shared server environment, something that has largely been replaced by the open_basedir directive.

Magic Quotes was more of the same, essentially escaping all quotes in the script input, this had security consequences if the application developed with magic quotes on was ever moved to an environment where it was turned off (think SQL injection), or conversely an application that was developed with magic quotes off would have some serious double-escaping problems if moved to an environment where it was on (something that I have seen several times).

Register Globals pulled in the variable from the request (Get,Post,Cookie, etc) into the global variable space, great for lazy programmers but quite of a security hole because it means that any data from the client could affect the script, for instance, if you were setting $admin to true if the logged in user was an admin but not setting it to false before the test, result? just putting ?admin=1 on the url would make you an admin.

 

Where Can I Use It

There is the unfortunate truth that many hosts are slow to release or update to new versions of PHP, many still only offer 5.2 simply because upgrading to 5.3 would break their clients websites. There hosts out there but you may have to hunt around to find one.

 

Further Reading

Sam

Sam graduated in 2008 with a degree in Computer Science and joined WDL in 2010 as a web developer. He enjoys programming and creating applications that are useful and efficient, but above all he loves solving problems.

More Posts - Meet the rest of the team

No comments yet.

Leave a comment!