PHP 5.4 – built-in web-server, closures, callable and other new features

This entry is part 4 of 5 in the series New in PHP 5.4

I am taking too long to close the New in PHP 5.4 series. PHP 5.4.3 is already released on 8th May 2012!!! Looking back, we are already familiar with the improvements in session handling, array syntax dereferencing and introduction of traits in PHP. Traits is an alternative approach for multiple inheritance. In another post, we read why we need traits.

In this post, we will read about the remaining new features that are introduced in PHP 5.4. We will begin with the new built-in web server.

Built-in Web Server in PHP 5.4

To run a PHP program, one has to configure the Apache HTTPD server. In PHP 5.4, there is an in-built CLI web-server that can be launched with a simple command. This web-server is, however, a tiny web-server meant for development purpose and should not be used in production environment.

Better Support for Closures

The concept of Closures was introduced in PHP 5.3. Closures are the anonymous functions that belong to an undocumented Closure class. When the function defined by the closure is executed, it has access to all the local variables that were in scope when it was created. PHP 5.4 adds new functions to the Closure class that help in controlling the anonymous function after it is created.

The Closure::bindTo() is modified work with $this and it now, accepts another argument. This new argument defines the new scope. The argument can either be a class name or an object if its class is used as the scope. We will read more about closures in a separate article.

Callable Typehint

PHP is loosely typed language, the traditional type-hinting with int and string is not supported. However, starting with PHP 5.1, type-hinting can be used to specify the expected data type for object and array. In PHP 5.4, there is a new “callable” typehint, which is used when a method takes a callback as an argument.

UTF-8 is Default Character-set

The default character has been changed from ISO-8859-1 to UTF-8. The setting is available in the php.ini file, but its default value is till “”. After enabling the UTF-8 character-set, the value is automatically sent in the headers. So, there’s no need to set that an additional HTML meta tag, or send UTF-8 compatibility header.

Instance Method Call

The Class members can be accessed on the instantiation. Example

$employee = (new Employee)->setName('Jim')->setDesignation('Designer');

The chaining was already supported.

Binary Representation

The binary values can be directly used in PHP code. A binary value has ‘0b’ prefixed to it. Example

echo 0b11111;

//Output: 31

JsonSerializable interface

PHP 5.4 introduces JsonSerializable interface with JsonSerialize abstract method. After implementing this method, we can independently set values used in JSON representation. So, when an object (of a class implementing JsonSerializable interface) is passed to json_encode(), you can control the output.

Support for Class::{expr}() syntax

Calling class/object methods is made more flexible with support for Class::{expr} syntax. So, now you can also access a class method by writing ClassName::method().

Short Tags are always On

The PHP short echo tags will work irrespective of the php.ini settings. So, the short echo tags will always work and you can safely write the short echo statement like .

Deprecated Methods and Features

In PHP 5.4, magic_quotes, safe_mode, register_globals are removed. The list of deprecated methods also include allow_call_time_pass_reference, define_syslog_variables, highlight.bg, register_long_arrays, zend.ze1_compatibility_mode, session_register, session_unregister(),session_is_registered(), session.bug_compat42, session.bug_compat_warn and y2k_compliance.

Improvement in Error messages to show more details about the error.

Several mysqli aliases have been removed. This includes mysqli_bind_param(),mysqli_bind_result() and mysqli_fetch().

The break() and continue() do not accept any parameters.

The sqlite extension is removed.

New Methods

hextobin(string $hex): coverts a hex representation of data to binary.

http_response_code(int $code): allows you to set or get the HTTP response code.

trait_exists(string $name [,bool $autoload]): determines whether a trait exists and, optionally, whether it should be autoloaded.

get_declared_traits(): returns an array of all declared traits.

class_uses(mixed $class [,bool $autoload=true]): returns the traits used by the given class

session_status(): returns the current session status

Other Improvements

Improved performance of @ (silence) operator.

Improved character support for htmlspecialchars() and htmlentities().

Added multibyte support by using zend.multibyte directive in php.ini.

In addition to the listed changes, a number of internal structures have been made smaller or eliminated completely. All the changes have resulted in 20-50% memory savings in large PHP applications. The latest version of PHP is already available for download from the official PHP website.

Series Navigation<< Traits in PHP 5.4 – Why we need Traits

  • http://twitter.com/refulz Refulz India

    Little typo… fixed after @mgdm pointed it out.