Tuesday, January 31, 2012

New Release PHP 5.4.0

New PHP Version 5.4.0 is planned to be released on February, 2 2012. It is a result of many months of development.

In the PHP Version 5.4.0 many features included. Some features are listed below.

  • Traits
  • Built-in Web server
  • Binary notation for integer values
  • Array short syntax
  • Annotations
Traits

PHP does not support multiple inheritance. This means that unlike languages such as C++, it is not possible to create one class by inheriting the behavior of multiple other classes.

However, PHP implements the support to have classes with multiple interfaces since version 5. This is a simple approach inspired in Java that avoids the ambiguity problems of C++ multiple inheritance implementation.

The problem of using either multiple inheritance or multiple interfaces is that it becomes easy for creating bloated classes that have much more inherited functions than they may really need in practice.

Traits provide a simpler alternative. You can define a trait practically the same way you define a class or an interface with whatever functions you need. Then you use them in a new class you want to have those functions.

Here is an example taken from the original proposal RFC document:

<?php

  trait ezcReflectionReturnInfo {
    function getReturnType() { /*1*/ }
    function getReturnDescription() { /*2*/ }
  }

  class ezcReflectionMethod extends ReflectionMethod {
    use ezcReflectionReturnInfo;
    /* ... */
  }
 
  class ezcReflectionFunction extends ReflectionFunction {
    use ezcReflectionReturnInfo;
    /* ... */
  }

 ?>
 
Built-in Web server

Although Apache has been losing popularity in the latest years for more efficient Web servers in use in production environments, it is still very popular among PHP developers that use it to test their applications in their development environments.

However, Apache is still too cumbersome and complicated to configure, especially when you just want to set it up for a simple development environment. Therefore, PHP 5.4 introduces a Web server that comes built in the PHP command line version.

This means that you only need to execute a simple PHP command and you have Web server running ready for you to test your applications without depending on setting Apache or any other external Web server.

Another interesting purpose of this built-in Web server is that you can develop browser based applications that run in the local machine and you only need the base PHP installation for that.

Here is an example command of how to run PHP to work as a Web server.

$ php -S localhost:8000

 Server is listening on localhost:8000... Press CTRL-C to quit.
 [Thu Mar  3 05:42:06 2011] ::1:56258: /
 [Thu Mar  3 05:42:06 2011] ::1:56259: /?=PHPE9568F34-A769-00AA02
 [Thu Mar  3 05:42:06 2011] ::1:56260: /?=PHPE9568F35-A769-00AA04
Binary notation for integer values

When you need to use literal values in your PHP code you can represent them in decimal, hexadecimal or octal. Now in PHP 5.4 you can also represent them in binary.

In octal you would need to prefix the value with a 0. For instance, 010 represents the number 8, not 10 as some may expect. In hexadecimal you would need to prefix the values with 0x, for instance 0x12 represents the number 18. In binary you need to prefix the number with 0b, for instance 0b101 represents the number 5.

Array short syntax

The popularity of JSON made it a common format for exchanging serialized data between code eventually written on different languages.

One basic difference between JSON and PHP literal value format is that arrays are represented by brackets instead the usual array() construct. Some people proposed to add support for a shorter syntax similar to JSON, but the proposal was not approved by PHP core developers.

Annotations

Annotations are a form of adding metadata to your code. The metadata information can be used by those tools for instance to produce additional support code, so you do not have to write such code manually. One common use is to generate PHP code or SQL statements to map data between class objects and database tables.

There were several proposals to implement annotations in PHP, very similar to the way they work in other languages like Java and C#. Those proposals ended up not being accepted or fully implemented to make into PHP 5.4.

Still, there are alternative approaches that consist in separate annotation parser tools that extract annotation metadata from PHP comments.


Tags: PHP,PHP Version 5.4.0,PHP New Releases,PHP Traits,PHP Annotations,PHP 5.4 Features,Upgrade PHP Version

1 comments:

Post a Comment