I am a programmer and architect (the kind that writes code) with a focus on testing and open source; I maintain the PHPUnit_Selenium project. I believe programming is one of the hardest and most beautiful jobs in the world. Giorgio is a DZone MVB and is not an employee of DZone and has posted 638 posts at DZone. You can read more from them at their website. View Full User Profile

What new feature in PHP 5.4 is the most important to you?

  • submit to reddit

Recently, the voting process for PHP 5.4 open to committers and users have been closed. We now have a clear picture of what will make the release and what will be left out. Some of these features (traits, web server) were already in, while other have been just voted and will be completed before the general availability of the release.

*At the end of this article you can vote for your preferred new PHP 5.4 feature.*

1. Traits

Traits are the most famous innovation in PHP 5.4: they consist in code fragments (a set of methods) that can be reused horizontally across classes. In other languages multiple inheritance or mixins address this issue, but traits are more similar to the latter: they do not provide state in the form of properties, but only an interpreter-assisted copy&paste of methods.

Moreover, traits have a conflict resolution mechanism: multiple traits can be used in the same class even if some method names are overlapping.
The RFC for traits talks also about grafts (entire classes reused instead), but not implemented in PHP 5.4 at the time of this writing.

RFC for traits: https://wiki.php.net/rfc/horizontalreuse

2. Arrays improvements: syntax and dereferencing

Arrays are the super glue of PHP, and of the most powerful constructs. A shorter syntax can now be used to define numerical and associative arrays. The new syntax only incorporated brackets as a short-hand, and has been defined as the PHP way. JSON syntax was proposed but voted against by the majority.

    array('foo', 'foo' => 'bar') == ['foo', 'foo' => 'bar']
); // true

Moreover, dereferencing of array elements from the result of function and method calls is now supported. The following code will work from PHP 5.4:


RFC for short syntax:


3. Built-in HTTP server

php -S localhost:8000 will start a small webserver for the current directory, for testing and development purposes. Additional options are a custom document root and a router script to be called at each request.

Although there are fears that someone will use this webserver in production somewhere, the focus is on quickly setting up a local environment for exploratory tests and experimentation. Next time you have to play with Ajax requests, you will be able to use PHP as the back end.

Official documentation: http://php.net/manual/en/features.commandline.webserver.php

4. Deprecations

PHP is being modernized by taking away (with backwards incompatibility) some old features which promote spaghetti code, and including new constraints.

  • Magic quotes options have been dropped.
  • The PHP namespace is now reserved for native code.
  • E_STRICT errors, covering for example correct usage of object-oriented constructs and deprecation warnings, are now covered by E_ALL. error_reporting(E_ALL) will show you really every error, warning and notice.

5. Closures

In PHP 5.3 closures are conceptually objects of the undocumented class Closure. While this type hint may be used, existing callbacks such as 'function_name' and array($object, 'methodName') are not considered acceptable by such a type hint. A new type hint will be added that cover all the cases.

This choice is consistent with new calling options: callbacks built with arrays can now be called directly as well:

$callback = array($object, 'methodName');
$callback(); // prior to PHP 5.4 you need call_user_func()

Closures rebinding is also an interesting feature that allows the usage of $this in closures defined inside objects, binding it to the object itself. This feature will eliminating the common hack:

$that = $this;
$closure = function (...) use ($that) { ... };

RFC for direct method calls: https://wiki.php.net/rfc/indirect-method-call-by-array-var

RFC for a Callable type hint (the actual name may change): https://wiki.php.net/rfc/callable

6. Upload progress

According to core developer Johannes Schlüter, the upload progress mechanism which used to be patched in PHP 5.3 will now be built in.

He makes an example of configuration and of a form:

<form action="upload.php" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="johannesupload" />
    <input type="file" name="file1" />
    <input type="file" name="file2" />
    <input type="submit" />

which allows you to poll the server multiple times and get back info about the progress (in this case from $_SESSION["upload_progress_johannesupload"]).

7. Miscellaneous

There are also some minor, not ground-breaking improvements you may appreciate:

  • the ability to disable POST data processing to save memory and CPU cycles - in case it is activated, raw data can still be read through the php://input stream.
  • binary notation: 0b10 is the same as writing 2.
  • implementing the JsonSerializable interface will make an object acceptable to json_encode() (while for json_decode() is not feasible to recreate the object.)

What's not in PHP 5.4

Not all the proposed features made it to PHP 5.4, due to downvotes or to their larger scope. This features won't be in PHP 5.4, and that's all we know about them.

  • Scalar type hints for method parameters.
  • Unicode/UTF-8 support in the language (e.g. strpos()).
  • Native annotations support: they'll still be embedded in the docblocks and parsed in userland.
  • Primitive types (integer, string...) as reserved words, removed for backward compatibility reasons.
  • Foreach list() support.

The final question

These groups of features have been discussed for months and they are now approved for PHP 5.4 (although not all implementations have been completed.) What potential new feature in PHP 5.4 is the most important to you?

29% (114 votes)
Arrays improvements (notation and dereferencing)
37% (145 votes)
Built-in HTTP server
7% (27 votes)
Deprecations (farewell magic quotes)
6% (23 votes)
Closures (type hint, direct calls to array callbacks, $this)
7% (28 votes)
Upload progress
11% (45 votes)
Miscellaneous (there's something very cool in there)
3% (12 votes)
Total votes: 394
Published at DZone with permission of Giorgio Sironi, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Mitch Pronschinske replied on Tue, 2011/07/19 - 11:02am

Great idea for a Poll! 

I'd also be interested in hearing what the community thinks about the features on their "wishlist" that may not make it into PHP 5.4.  Is there anything not mentioned here that you think PHP needs?

Giorgio Sironi replied on Tue, 2011/07/19 - 1:54pm

PHP is loading up support for every programming paradigm with closures, objects, even Gotos. But since we are talking about improved array syntax, some syntactyc sugar for anonymous functions too would be helpful, and will make them similar to Smalltalk and Ruby blocks.

Francesc Rosàs replied on Tue, 2011/07/19 - 8:09pm

What shocks me is it seems traits use duck typing to enforce its requirements. Maybe it is because I'm so used to interfaces but I see duck typing more error-prone. What do you think about it?

BTW I'm not sure whether to vote traits or closures now they support $this.

Rick Buitenman replied on Wed, 2011/07/20 - 5:27am

Surprised traits isn't the outright winner here. Whereas the rest is nice, traits will likely have a major impact on the way reusable code, including the big frameworks and libraries, will be designed in the future.

IMO this is the only feature that adds fundamental new possibilities for designing better code.

Giorgio Sironi replied on Wed, 2011/07/20 - 5:46am in response to: Francesc Rosàs

PHP is halfway between duck typing and interfaces: it supports both styles. I'm installing PHP 5.4 in a VM to play around and see if traits result in a mess or in less duplication.

Giorgio Sironi replied on Wed, 2011/07/20 - 5:47am in response to: Rick Buitenman

It's true that many other languages have mixins; it's early to see if they will be abused or if they are a design improvement.

Carla Brian replied on Sun, 2012/04/08 - 7:18pm

I love their new features. I am new to this so I need more practice about its tools. - Marla Ahlgrimm

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.