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

article_image.axd_.pngSource: DZone; 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.

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.

var_dump(
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:

$object->method()[0]->otherMethod();

RFC for short syntax:
https://wiki.php.net/rfc/shortsyntaxforarrays

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:

" value="johannesupload" />

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.

    PHPDevShell © 2010 - All rights reserved.