Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News PHP 5.4 Drops Register Globals, Adds Traits

PHP 5.4 Drops Register Globals, Adds Traits

Leia em Português

This item in japanese

PHP 5.4, the first major update since 2009, was finalized this month. This release includes several language enhancements including support for Traits as well as the removal of some controversial features.


PHP, like Java and .NET, uses a single inheritance model. While this is enough for most use cases, there are times when common coding patterns need to be repeated in otherwise unrelated classes. (.NET’s Dispose method is another good example of this sort of boiler-plate code.) To address this PHP 5.4 offers Traits.

Traits are collections of methods that do not belong to an actual class. There is no way to create an instance of a Trait or call its methods directly. Instead Traits must be incorporated into a real class. In terms of precedence, a trait method will override an inherited method. Likewise a method declared in the current class will override the trait method.

Traits have equal precedence, so by default it is an error to use two traits in the same class if the traits have overlapping method names. This error can be avoided if the “insteadof” and “as” operators are used to manually resolve conflicts. The “as” operator can also alter the visibility of a trait.

Trait methods may be abstract, which is useful when the trait can only be partially generalized. Traits can also expose static variables, though each class gets its own copy. Properties declared on traits cannot be declared in a class that uses the trait.

Other Syntax Improvements

Other syntax improvements include

  • Arrays may be now declared with square brackets instead of the “array” function. While slightly shorter, it has no other effect.
  • Array dereferencing now works on the results of a function. Previously the result would have to be first placed in the temporary variable: $FirstName = GetNames()[0]
  • Closures in PHP 5.4 may use the “$this” pointer, which was not previously allowed.
  • Class member access on instantiation is now allowed: (new CustomerService)->Delete(customerId).
  • Binary numbers using the “0b” prefix.

Command Line Web Server

PHP 5.4 offers its own web server that can be launched from the command line. This web server is designed for development purposes only; it does not replace your production web server.

Deprecated Features

The much maligned Register Globals feature has been removed entirely from PHP. This feature has been a well-known source of security vulnerabilities for over a decade and was turned off by default in 2002. PHP 5.3, released in 2009, marked the feature as deprecated but by then most developers should have already ceased using it.

Another disliked feature removed from PHP was Magic Quotes. Magic Quotes was an attempt to automatically escape strings in an attempt to avoid SQL Injection attacks. But since the use and method of string escaping is context-specific, this feature often caused more problems than it solved. Like Register Globals, this was deprecated in 2009.

Break and continue statements in PHP allow a parameter to indicate how many levels to break out of. If not supplied, it breaks from the innermost loop just like VB, C#, or Java. Prior to PHP 5.4 developers could pass in a variable to the break statement, now it requires a constant.

PHP allows passing arguments by reference. In early versions you could indicate the variable was passed by reference by decorating the call site. With PHP 5.4, this option has been eliminated. Instead, modern PHP programming requires that the indicator has to be on the function declaration. Unlike C#, you never need an indicator at both the declaration and call site.

Rate this Article