Technology

PHP 5.4.0 Official Release: What's New in PHP 5.4

Discover the major updates and new features introduced in PHP 5.4.0, including Traits, short array syntax, and a built-in web server, alongside deprecated and removed functions.

Sangrak

The long-awaited official release of PHP 5.4.0, packed with numerous new features, is finally here. Previously, only the Release Candidate (RC) versions were available. According to the official PHP website, the leap from versions 5.1-5.3 to 5.4 represents a significant upgrade in terms of updates and improvements.

Deprecated Features in PHP 5.4.x

Some functions have become deprecated in PHP 5.4.x, indicating they should no longer be used as they might be removed in future versions:

  • mysql_list_dbs()
  • get_magic_quotes_gpc() will always return false
  • get_magic_quotes_runtime() will always return false
  • set_magic_quotes_runtime() will trigger an error
  • mcrypt_generic_end() This suggests moving away from using magic quotes altogether.

Functions Completely Removed

The following functions have been entirely removed in PHP 5.4:

  • define_syslog_variables()
  • import_request_variables()
  • session_is_registered(), session_register(), and session_unregister()
  • Functions related to mysqli and mysqli_stmt

New Features

PHP 5.4 introduces several exciting features, including Object-Oriented Programming (OOP) Traits, short array syntax, and a development-oriented built-in web server.

Short Array Syntax

One of the most noticeable new features is the ability to use a shorter syntax for arrays. Previously, arrays were defined as follows:

$var = array(
	1 => "Example1",
	2 => "Example2",
	3 => "Example3"
);

Now, they can be defined more succinctly:

$var = [
	1 => "Example1",
	2 => "Example2",
	3 => "Example3"
];

This syntax reduces visual clutter. Additionally, PHP 5.4 allows for more flexible syntax in object chaining and instantiation.

Traits

Traits offer a way to use 'inheritance without restrictions' across different classes:

trait Hello {
    public function sayHello() {
        echo 'Hello ';
    }
}

trait World {
    public function sayWorld() {
        echo ' World';
    }
}

class MyHelloWorld {
    use Hello, World;
    public function sayExclamationMark() {
        echo '!';
    }
}

Built-in Web Server

Launching a development server is now straightforward:

$ cd ~/public_html
$ php -S localhost:8000 -t ./

To verify if the server is running in this mode, php_sapi_name() will return "cli-server".

New Functions

PHP 5.4.0 introduces several new functions and constants, enhancing its functionality and compatibility with various protocols and standards. Notable additions include hex2bin, http_response_code, get_declared_traits, among others, and a plethora of new constants across different PHP extensions like Curl, LibXML, OpenSSL, and more.

Miscellaneous Updates

  • array_combine() now returns an array instead of false.
  • call_user_func_array() no longer supports call-time pass by reference.
  • Short open tag <?= is always allowed.
  • New hash algorithms: fnv132, fnv164, joaat.
  • Support for dynamic class method calls.
  • Closure now supports $this.

PHP 5.4.0's release marks a significant step forward in the PHP language's evolution, introducing features that enhance its functionality, improve developer productivity, and ensure greater code efficiency.

More on this topic