PHP Classes

PHP 5.4 Features: Shall you Upgrade to the newer PHP Version?

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog PHP 5.4 Features: Sha...   Post a comment Post a comment   See comments See comments (17)   Trackbacks (1)  

Author:

Viewers: 62

Last month viewers: 14

Categories: PHP community, PHP opinions

PHP 5.4 is being released after many months of development and tests.

Read this article to get a brief overview of the features of this new release and learn about criteria that you may follow to decide if and when you should upgrade to the new version.




Loaded Article

UPDATE: Fixed the information that the array short syntax was not approved, which was not accurate. It was due to misinterpretation of the vote count.

Contents

PHP 5.4 Features

Discontinued Features

Left Out Features

Shall I Upgrade to PHP 5.4?

Conclusions

PHP 5.4 Features

PHP 5.4PHP 5.4.0 is planned to be released on February, 2 2012. By the time you are reading this, it may already been out. It is a result of many months of development.

Many features were proposed for this release. Some made into this version, others did not make it at least for now. So, now you may be wondering which interesting features really made it. Let me tell you more about some of the more interesting features present in this release.

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. The proposal was not approved by many PHP core developers but many users voted for it. So now it is possible to define an array like this:

 $a = [1, 2, 3];
$b = ['foo': 'orange', 'bar': 'apple', 'baz': 'lemon'];

Discontinued Features

Some PHP releases discontinue past features. That happens especially in more important releases like this. Usually features are discontinued after have been made deprecated in past versions.

In PHP 5.4 magic quotes is a feature that was discontinued. It used to be enabled if you had certain options set in php.ini file.

It was meant to automatically escape values for use in SQL queries. The problem is that the use of that feature made third party PHP components unreliable, as they would need to check if the option was set to determine if they need to escape the values to use SQL queries.

Furthermore, correct literal value escaping depends on the database you are connecting to. So it is more reliable to have the magic quotes options disabled and escape the values explicitly, eventually using database specific functions for that purpose, like for instance mysql_real_escape_string().

Left Out Features

Many features were proposed for PHP 5.4 but not all made to this version either because they were not approved by core developers, or their proponents did not develop them enough to make it into a release. Here are some of the most important features that did not make into PHP 5.4.

APC caching extension

PHP compiles the code into Zend opcodes before executing. This makes PHP run much faster than when it was an interpreted language before the PHP 4 days.

However, the opcode compilation process still consumes a significant amount of time, specially if you code uses a lot of external component scripts. This overhead can be avoided if you use a caching extension. If you are using PHP in a busy Web server, using an opcode caching extension is mandatory.

There are many caching extensions. Some are Open Source, others are commercial. APC is an Open Source caching extension developed by several core PHP developers.

Despite the importance of using an opcode caching extension, in PHP 5.4.0 APC is still not shipping in the main PHP distribution.

It was planned initially to make it in PHP 6, but the original plans for that major release were canceled. Then it was anticipated for PHP 5.4, but at least for PHP 5.4.0, APC is claimed to not be ready for this version.

This is not good news for PHP. Frequently we see language benchmark comparing PHP with other languages used on the Web. Very often the PHP installation used in the benchmarks is running without an opcode cache, while the other languages use their own caching mechanisms.

It is an unfair comparison but many people are not aware about these details so they believe PHP is really slower. PHP often appears to be losing because the benchmarks are not done with PHP running with a caching extension.

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.

Shall I Upgrade to PHP 5.4?

By now you may be wondering if it is worth to upgrade to PHP 5.4. Here is some advice that you may want to consider. Some of the recommendations are so obvious, that maybe they do not really need to be given, but they are given anyway so you be certain that this is just common sense.

No, if you do not really need the new features

It is basic common sense to never change the team that is winning. If you looked at PHP 5.4 features and did not see anything you may be interested in using, simply do not bother to upgrade.

If you insist just because you want to ride on the top of the latest wave, chances are that your PHP code may break due to changes that you were not aware.

No, if you need unsupported extensions

If you need some extensions that do not come built-in PHP, like for instance a caching extension as mentioned above, chances are that such extensions may not yet be ready for this new version.

So, it is advised to not upgrade at least until such extensions are upgraded to work with PHP 5.4, or maybe do not upgrade at all until there are replacement extensions for the same purposes.

Give it some time

I always say: new versions, new bugs. This means that it is practically guaranteed that this new version will have bugs that may break your code.

Despite all the testing PHP 5.4 has gone through, there are always bugs that will only be found after it is released officially and the masses start trying it.

The most obvious bugs will be fixed soon. So, if you are really determined to upgrade it in production, be wise and wait for a while, maybe at least one month. That will give time for core developers to fix the most outstanding bugs. 

Try it in your development environment

Even if you do not upgrade, nothing should stop you from using PHP 5.4 in your development environment. In the worst case, if it breaks your code, you can always downgrade without affecting your site audience, because only you or your team should be using that development version.

Furthermore, with the new version support to make PHP 5.4 work as the Web server, you do not even need to uninstall an older version that you may be using. Just run it from the command line and see if all works.

Conclusions

PHP 5.4 does not really constitute a major PHP upgrade but it is always good for the PHP community to see a new version being released, as it shows good signs of vitality of the PHP development. Whether you will be using this new version or not, it is a different story.

In any case, congratulations to Stas Malishev, David Soria Parra (the release managers) and all other developers involved in making this PHP release happen. Liking it a lot or not, it is always a non-trivial job that requires great competence to put together a new release of such a popular language like PHP. Kudos for everybody that gave their best to bring PHP 5.4 alive.

So, what about you? Did you like teh features of this new PHP version? Do you plan to use it? Do you have a different criteria to decide from the presented above? Which other features not mentioned in this article did you find relevant? Feel free to post a comment giving your points of view on this.




You need to be a registered user or login to post a comment

Login Immediately with your account on:



Comments:

8. Giv PHP 5.4 a chance - MINESH GHIMIRE (2012-02-25 05:35)
lets try it out asap so as to trace bugs therein...... - 0 replies
Read the whole comment and replies

7. Short Array - Hari K T (2012-02-01 08:27)
In Implemented List... - 1 reply
Read the whole comment and replies

6. Still coding and hoping - Roger Cable (2012-02-01 08:27)
Hopes for better... - 1 reply
Read the whole comment and replies

5. Errata - João Bruni (2012-02-01 08:08)
Typo: "wait for a whole" = "wait for a while"... - 1 reply
Read the whole comment and replies

4. See this article for list of all major changes in PHP 5.4 - Artur Graniszewski (2012-02-01 08:07)
There is much more changes than covered by this article.... - 1 reply
Read the whole comment and replies

3. Short Array Notation - Tyler Sommer (2012-02-01 08:05)
PHP 5.4 will ship with Short Array Notation... - 2 replies
Read the whole comment and replies

2. Not very good news. - Nader Magdi Moussa (2012-02-01 08:04)
Great article.... - 1 reply
Read the whole comment and replies

1. Thanks Manuel - John Edwards (2012-02-01 08:04)
Thanks Manuel... - 2 replies
Read the whole comment and replies


Trackbacks:

1. Nuevas características de PHP 5.4 (2012-01-31 02:34)
La nueva versión de PHP, PHP 5.4.0, tiene prevista su salida el día 2 de Febrero de 2012, después de muchos meses de desarrollo...



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog PHP 5.4 Features: Sha...   Post a comment Post a comment   See comments See comments (17)   Trackbacks (1)