Php

Top 10 new features in PHP 5.4

PHP 5.4 includes a host of new features and many bug fixes. The latest version of PHP was released last 1st. March, between general improvements in version found eg performance improvements, reduced memory consumption, base code "cleaner" because functions were eliminated "deprecated", among others.


WARNING !!: A major issue for users of Windows XP and 2003 systems is that PHP 5.4 is the last version that provides executables for these versions of the Microsoft operating system.

What we prepare in this article is an introductory guide to the new features offered by PHP 5.4, so you can realize the full potential of PHP and stay updated.

PHP 5.4 - new features

1. Traits


Its appearance is due to a "lack" of PHP which is multiple inheritance, ie a PHP class can only inherit functionality from a single parent class; Traits is how PHP developers found to overcome the above problem and allows the reuse of code from various classes.

The traits are structurally similar to classes but can not be instantiated in each class can use the number of traits you set out to simulate multiple inheritance, see an example:

<? Php
ClaseUtilA trait {
    getMetodoUtilA1 function () {/ * 1 * /}
    getMetodoUtilA2 function () {/ * 2 * /}
}
ClaseUtilB trait {
    getMetodoUtilB1 function () {/ * 1 * /}
    getMetodoUtilB2 function () {/ * 2 * /}
}
 
ClasePrueba class {
    use ClaseUtilA, ClaseUtilB;
    // I can use as many traits as they are needed
    / * ... * /
}
?>

 


2. Syntax simplified arrays


You can now create arrays in a similar way as we do in javascript, eg:

<? Php
    $ Test = ['one' => 1, 'two' => 2 'tres' => 3];
    $ Test = [1,2,3];
?>

 


3. Web server integrated testing


With a simple command line you can start a web server to test your scripts without having installed Apache obviously not be used for production environments.

$ Cd ~ / public_html
$ Php -S localhost: 8000

 


4. Array dereferencing


Functionality "array desreferenciamiento" or array dereferencing was added; often create a lot of temporary variables to store data just did not use it ... with this new feature code becomes more compact and clear; see an example:

<? Php
// We can now write this ...
fruit function ()
{
    return array ('a' => 'apple', 'b' => 'banana');
}
Echo fruit () ['a']; // <--- This is array dereferencing
// Shows 'apple'
 
// Before we did this ...
fruit function ()
{
    return array ('a' => 'apple', 'b' => 'banana');
}
 
Fruits fruit $ = ();
echo $ fruits ['a'];
// Shows 'apple'
?>

 

5. Tag short opening


We now have available whenever the short tag

<? =


regardless of the value of the short_open_tag php.ini option; This tag is used in templates or templates, it is used to indicate the opening of PHP code and simultaneously use the "echo", eg:

<? = $ Value?>
// Is equivalent to
<? Php echo $ value; ?>

 

 


6. Accessing class members during creation


Another feature that aims to simplify code example:

<? Php
// Before we wrote
$ Object = new ClaseUtil ();
$ Value = $ object-> funcionUtil ();
 
// We can now write
$ Value = (new ClaseUtil) -> funcionUtil ();
?>

 


7. Monitoring the process of uploading files in the session


Now we know the progress of uploading a file via a key in $ _SESSION, this is a very interesting feature. We can even cancel a climb that is in progress. Very useful to generate progress bars known.

8. Binary representation of integers


Now we can write the integers in binary notation, for this we use "0b" prefix, eg:

<? Php

echo "Number". 0b10; // Display "Number 2" (10 binary = 2)

echo "Number". 2; // Display "Number 2"
?>

 


9. New classes and interfaces


Several new classes and interfaces were added in PHP 5.4, some interesting to know more about are: JsonSerializable, SessionHandler, SNMP, SPL, etc. See the complete list of new classes and interfaces.

10. New functions, methods, and other ...


In PHP 5.4, a significant number of methods, new functions and global constants, as well as several changes in the PHP.ini file is added, it is best to review the lists of changes to not miss anything.

Anyway you leave a very small list with some interesting new features:

http_response_code (); // Returns the HTTP response code
session_status (); // Allows to know the state of the session
trait_exists (); // To see if there is a particular trait and autocargarlo if necessary

Share this post

Leave a comment