Advertisement

PHP 5.4 is Here! What You Must Know

by

PHP 5.4 is here; the next major step forward since version 5.3 - keeping PHP 6 (full Unicode support) on hold for now. The latest enhancements significantly improve its elegance, while removing deprecated functionality, resulting in a dramatic optimization of the runtime (up to 20% more speed and memory usage reduction).

brace yourself

New Features and Improvements

Some of the key new features include traits, a shortened array syntax, a built-in webserver for testing purposes, use of $this in closures, class member access on instantiation, <?= is always available, and more!

PHP 5.4.0 significantly improves performance, memory footprint and fixes over 100 bugs. Notable deprecated/removed features include register_globals, magic_quotes (about time) and safe_mode. Also worth mentioning is the fact that multibyte support is enabled by default and default_charset has been changed from ISO-8859-1 to UTF-8.

Content-Type: text/html; charset=utf-8 is always sent; so there's no need to set that HTML meta tag, or send additional headers for UTF-8 compatibility.


Traits

The best demonstration of traits is when multiple classes share the same functionality.

Traits (horizontal reuse/multiple inheritance) are a set of methods, which are structurally similar to a class (but can't be instantiated), that can enable developers to reuse sets of methods freely in several independent classes. Because PHP is a single inheritance language, a subclass can inherit from only one superclass; that's where traits come to play.

The best use of traits is demonstrated when multiple classes share the same functionality. For instance, imagine that we are building some website, and need to use both the Facebook and Twitter APIs. We build two classes which, in common, have a cURL wrapper function/method. Instead of performing the classic copy & paste of that method - to be used in two classes - we use Traits (copy & paste, compiler style). This way, we make reusable code, and follow the DRY (Don't Repeat Yourself) principle.

Here's an example:

Got it? No? Here is the simplest example!

If you have any question about traits, please post a note in the comments section below.

Important Tip: The magic constant for traits is __TRAIT__.


Built-in CLI Web-Server

In web development, PHP's best friend is Apache HTTPD Server. Sometimes, though, it can be overkill to set up httpd.conf just to use it within a development environment, when you really need tiny web server that can be launched with a simple command line. Thankfully, PHP 5,4 comes with a built-in CLI web server.

The PHP CLI web server is designed for developmental purposes only, and should not be used in production.

Note: The instructions below are for a Windows environment.

Step 1 - Create Document Root Directory, Router File and Index File

Go to your hard drive root (assuming C:\). Create a directory/folder, called public_html. Create a new file within this folder, and name it router.php. Copy the contents below, and paste it into this newly created file.

Now, create another file, called index.php. Copy the contents below and save the file.

Open your php.ini file (it is located in the PHP install directory - e.g. C:\php).

Find the include_path settings (it is located at ~708th line). Add C:\public_html to the end of the string between quotes, separate by a semicolon. The final result should look like:

Save and close the file. On to next step.

Step 2 - Run Web-Server

Open the command prompt (Windows + R, type in cmd, hit Enter); you should see something like this, depending on your Windows version.

Change your current directory to the PHP installation by following the example below:

Here comes the most important part - running the web-server. Copy...

... and paste it in the command prompt (right mouse button, click Paste to paste). Hit Enter. If all goes well, you should see something similar to what's shown below. Do not close the command prompt; if you do, you will exit the web-server as well.

Open up http://localhost:8080/index.php in your browser and you should see:

Voila! That's it, happy coding!

Tip 1: Make a php-server.bat file with the following contents: C:\php\php -S 0.0.0.0:8080 -t C:\public_html router.php. Double click it, and, now, the server is up and running!

Tip 2: Use 0.0.0.0 instead of localhost if you anticipate that your server will be accessed from the internet.


Shorter Array Syntax

PHP 5.4 offers a new shorter array syntax:

Please note that "old" method is still in use and always will be. This is simply an alternative.


Array Dereferencing

No more temporary variables when dealing with arrays!

Let's imagine that we want to retrieve the middle name of Alan Mathison Turing:

Sweet; but it wasn't always this easy. Before 5.4, we had to do:

Now, what if we want to get the last name (last element in array):

This works fine, however, it will throw a E_STRICT (Strict Standards: Only variables should be passed by reference) error, since it became part of E_ALL in error_reporting.

Here's a slightly more advanced example:


$this In Anonymous Functions

this is anonymous functions

You can now refer to the object instance from anonymous functions (also known as closures) by using $this.

Note that this could be achieved prior to 5.4, but it was overkill.


<?= is Always On

Regardless of the php.ini setting, short_open_tag, <?= (open PHP tag and echo) will always be available. This means that you can now safely use:

...in your templates instead of...


Binary Number Representation

binary php

There are only 0b10 kinds of people;
those who understand binary and those who don't.

From now on, integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation, optionally preceded by a sign (- or +). To use octal notation, precede the number with a 0 (zero). To use hexadecimal notation, precede the number with 0x. To use binary notation, precede the number with 0b.

Example: representation of number 31 (decimal).


Callable Typehint

wuts dat

Typehinting is for those who desire to make PHP a stronger typed language. Type Hints can only be of the object and array type since PHP 5.1, and callable since PHP 5.4. Traditional type hinting with int and string isn't yet supported.


Initialized High Precision Timer

$_SERVER['REQUEST_TIME_FLOAT'] has been added, with microsecond precision (float). This is useful when you need to calculate the execution time for a script.


__destruct() (or Summary)

Overall, PHP 5.4 offers numerous improvements. Now, it's up to you to grab a fresh copy from php.net, and make quality Object-Oriented PHP code!

What do you think PHP 5.5 will bring us, and what do you expect?

Advertisement