×

php up to date

WHAT EVERY DEVELOPER MUST KNOW ABOUT PHP 5.4

WHAT EVERY DEVELOPER MUST KNOW ABOUT PHP 5.4



1. Trait Support
PHP is a single inheritance language. That is, subclasses can only inherit from a single superclass, which limits the methods that can be used across various independent classes. Hence, PHP 5.4 includes support for traits – a long overdue feature that dramatically improves the flexibility of the language and reduces repetitive, wasteful code.

Traits are essentially sets of methods that can be reused across various classes. The classic approach would entail creating two instances of the same code if a method is to be adopted across two classes. But with traits, you can create a single method and call on it in two different classes without repeating the code, which makes for leaner, more elegant code.



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.

Short array syntax has been defined the PHP way in lieu of the formerly-proposed JSON syntax. Hence, an array can be defined thus:

$a = [1, 2, 3, 4];
$b = ['one' => 'two', 'three' => 'four'];
Array dereferencing, on the other hand, removes the need to define temporary variables and streamlines the code. Thus, while in earlier versions of PHP, you had to store the value from a function in a variable, and then use the variable, in PHP 5.4, you can call upon the
stored value directly. For example, earlier, you had to do this:

$food = explode(",", "pizzahut,burgerking,kfc,mcdonalds");
echo $food[3]; // mcdonalds
Now you can simply do this:

echo explode(",", "pizzahut,burgerking,kfc,mcdonalds")[3]; // mcdonalds
3. $this Support in Closures
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.

Example and explanation of closures on the PHP.net manual

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:

$ cd ~/public_html
$ php –S localhost:8000
Please note that this CLI web-server is meant for testing purposes only and cannot be used for actual production.

5. < ?= Support is Always Available
The short_open_tag setting in php.ini allows for the use of < ?=. PHP 5.4 streamlines the code and includes support for < ?= by default. This essentially means that instead of writing:

<?php echo $testing ?>
You can use

<?=$testing?>
SHARE

blogger

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

Thanks for your valuable feedback