What’s New in PHP 5.5 Part - III




Class Name Resolution

Since the introduction of namespaces in PHP 5.3, it has become common practice to use extensive namespacing to organize classes in PHP projects. However, until now it has been difficult to retrieve a fully-qualified class name as a string. Consider the following code:

<?php
use NamespacedClassFoo;

$reflection = new ReflectionClass("Foo");

This will fail as PHP will attempt to use the global Foo class instead of the namespaced class. In PHP 5.5, it is now possible to retrieve the full namespaced class name as a string using the class keyword:

<?php
use NamespacedClassFoo;

$reflection = new ReflectionClass(Foo::class);

This will now work as intended, as Foo:class will resolve to Namespaced\Class\Foo.

Changes to foreach

The list() construct in PHP allows the values of an array to be easily assigned to variables. For example:
<?php
$values = ["sea", "blue"];

list($object, $description) = $values;

// returns "The sea is blue"
echo "The $object is $description";


It’s now possible to use list() with multidimensional arrays within foreach loops. For example:

<?php
$data = [
    ["sea", "blue"],
    ["grass", "green"]
];

foreach ($data as list($object, $description)) {
    echo "The $object is $descriptionn";
}

/* Outputs:
The sea is blue
The grass is green
*/

This is a powerful new feature that has the potential to make iterating through nested arrays much easier and cleaner.

foreach loops can now also handle non-scalar values as iterator keys, which means that element keys may have values that are not strings or integers.


Conclusion

PHP 5.5 offers many improvements to facilitate PHP development. In addition to new features, a long list of bugs have been resolved in this release (see the changelog for details), and various optimizations and enhancements have been made to improve performance and stability.