With the PHP 5.4.0 RC3 released last week, 5.4.0 will be arriving soon. As well as a whole bunch of bug fixes, there are a few new interesting features. Here is a selection of some of the more interesting or handy features
Magic quotes removed
The 4th August PHP 5.4.0 Alpha 3 was the version where they removed magic quotes. (Finally!)
- Removed features:
. Removed magic_quotes_gpc, magic_quotes_runtime and magic_quotes_sybase
ini options. get_magic_quotes_gpc, get_magic_quotes_runtime are kept
but always return false, set_magic_quotes_runtime raises an
E_CORE_ERROR. (Pierrick, Pierre)
@ Operator speed increase
Using the @ operator (silence) to ignore any errors has been given some speed increases, which should help scripts that use a lot of them.
$file = @file_get_contents('/invalid'); // faster in 5.4.0
$GLOBALS initialized only if used
The $GLOBALS variable is only initialised if it is used. May cause bugs in existing scripts.
Array dereferencing support added
A neat trick that means you don’t have to store the returned result to a temporary variable when you want to access a certain array item. In < php 5.4.0 you had to do this:
function example() {
return array(
1 => 'first result',
2 => 'result you want'
);
}
$tmp = example();
echo $tmp[2];
But in php 5.4.0 you can do this:
function example() {
return array(
1 => 'first result',
2 => 'result you want'
);
}
echo example()[2];
Binary value support added
Binary values can be used in php, for example:
$num = 0b001010
Support for traits has been added
Traits are a new feature to PHP that lets you easily reuse code in classes. Because a class can only inherit one class (although of course the parent class(es) could inherit more), using traits allows you to easily reuse sets of methods in different classes.
Traits are written in a similar way to classes, but they cannot be instantiated.
Example of a trait:
<?php
trait example_trait {
function example_function_from_trait() {
echo 'example';
}
}
class example_class1 {
use example_trait; // <<--
function __construct() {
$this-&gt;example_function_from_trait();
// uses function from the trait
}
}
?>
Traits have now been added to php 5.4.0.
Using Anonymous Functions when stored as an array
In the previous version of PHP you could store anonymous functions in arrays, but to call the anonymous function it required a temporary variable.
The old way of doing this (PHP 5.3.x):
<?
// set up the anonymous function, stored in an array
$anon_funcs = array();
$anon_funcs['sayhello'] = function()
{
echo 'Hello World!';
}
?>
To call it in php 5.3.x:
<? $tmp = $anon_funcs['sayhello']; $tmp(); //$anon_funcs['sayhello']() would not work ?>
But now you don’t need that temporary variable, so in php 5.4.0 you can call it by just doing this:
<? $anon_funcs['sayhello'](); ?>
Which makes code shorter and neater.
Short array syntax added
No need for array(), you can just use [ and ]
$array = [123,456,789101112];
Built in web server (for testing)
PHP now has a new SAPI module (cli-server) which is a built in web server that is intended for testing purposes. Here are some examples of using PHP’s built-in web server (from their docs).
It is run from the command line with
php -S localhost:8000
That command will use the document root of the current working directory. (So do the following command first to move to the directory you want to serve)
$ cd ~/public_html $ php -S localhost:8000
If you want to start with a different document root for example ~/webfiles/), use this command:
php -S localhost:8000 -t ~/webfiles/
Then visit http://localhost:8000/ in your browser to view the pages.
You can use router scripts, which will ‘redirect’ to the requested file.
If you use a router script and want to return the requested file (for example if a user goes to http://localhost:8000/image.jpg, and you want to show image.jpg) you must return false. This example should make sense –
<?php
// router.php
if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER['REQUEST_URI']))
return false; // serve the requested resource as-is.
else {
echo 'Welcome to PHP'
}
?>
When using the following command, any image requests (for example http://localhost:8000/image.png) will show the image (if it exists of course), because the script will return false. If it isn’t a png/jpg/jpeg/gif request, it will echo ‘Welcome to PHP‘.
$ php -S localhost:8000 router.php
E_ALL now includes E_STRICT
E_ALL never used to include E_STRICT, you had to explicitly enable it if setting E_ALL. In PHP 5.4.0, E_STRICT is included in E_ALL.