Friday, September 14, 2012

What Every Developer Must Know About PHP 5.4 ?


Some of the key new features include traits, a shortened array syntax, a built-in webserver for testing purposes, use of $this in closures, class member access on instantiation, <?= is always available, and more!
PHP 5.4.0 significantly improves performance, memory footprint and fixes over 100 bugs. Notable deprecated/removed features include register_globals, magic_quotes (about time) and safe_mode. Also worth mentioning is the fact that multibyte support is enabled by default and default_charset has been changed from ISO-8859-1 to UTF-8.

1. Trait Support
As of PHP 5.4.0, PHP implements a method of code reuse called Traits.
Traits is a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. The semantics of the combination of Traits and classes is defined in a way which reduces complexity, and avoids the typical problems associated with multiple inheritance and Mixins.
A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way. It is not possible to instantiate a Trait on its own. It is an addition to traditional inheritance and enables horizontal composition of behavior; that is, the application of class members without requiring inheritance.
.
2. Improvements to Arrays
PHP 5.4 includes two significant improvements to arrays – support for short array syntax and dereferencing of arrays from function and method calls. Both these changes make the code easier to read and manage.
No more temporary variables when dealing with arrays!
Let’s imagine that we want to retrieve the middle name of Alan Mathison Turing:

echo explode(' ', 'Alan Mathison Turing')[1]; // Mathison
Sweet; but it wasn’t always this easy. Before 5.4, we had to do:

$tmp = explode(' ', 'Alan Mathison Turing');
echo $tmp[1]; // Mathison
Now, what if we want to get the last name (last element in array):

echo end(explode(' ', 'Alan Mathison Turing')); // Turing
This works fine, however, it will throw a E_STRICT (Strict Standards: Only variables should be passed by reference) error, since it became part of E_ALL in error_reporting.
Here’s a slightly more advanced example:


function foobar()
{
    return ['foo' => ['bar' => 'Hello']];
}
echo foobar()['foo']['bar']; // Hello

 3. $this Support in Closures
You can now refer to the object instance from anonymous functions (also known as closures) by using $this.
Anonymous or unnamed functions are called closures. These functions are very useful as the value of callback parameters. Prior to PHP 5.4, referring to object instances from closures required lengthy workarounds. With support for $this, you can call on any object property in any anonymous function, eliminating the need for hacks.

4. Built-in Web Server
Since the focus of PHP 5.4 is to streamline the development process, it includes a built-in web server in CLI mode on port 8000 to facilitate faster development and testing, thereby eliminating the need to set up an Apache HTTPD server. This server can be called on by using a simple command:
Open the command prompt (Windows + R, type in cmd, hit Enter); you should see something like this, depending on your Windows version.
Change your current directory to the PHP installation by following the example below:
Here comes the most important part – running the web-server. Copy…
… and paste it in the command prompt (right mouse button, click Paste to paste). Hit Enter. If all goes well, you should see something similar to what’s shown below. Do not close the command prompt; if you do, you will exit the web-server as well.

 5. < ?= Support is Always Available
Regardless of the php.ini setting, short_open_tag, <?= (open PHP tag and echo) will always be available. This means that you can now safely use:
<?=$title?>…in your templates instead of…<?php echo $title ?>

No comments:

Post a Comment