• Frank Tyger

    Doing what you like is freedom. Liking what you do is happiness.

php 7 what’s new

php 7 what's new

2015 has been an important year for PHP. Eleven years after its 5.0 release, a new major version is finally coming our way! PHP 7 is scheduled for release before the end of the year, bringing many new language features and an impressive performance boost.

But how will this impact your current PHP codebase? What really changed? How safe is it to update? This post will answer these questions and give you a taste of what’s to come with PHP 7
If you’re a website developer/owner running their sites on WordPress (or on any other PHP-powered CMS such as Joomla, Drupal, Magento), then you’ve landed to the right blog post. This informative guide will not only give you a taste of what’s new coming in PHP 7 but also will answer all your questions related to the new PHP version!

Wordpress-php7-performance

Performance is undoubtedly the biggest reason why you should upgrade your servers as soon as a stable version is released. The core refactoring introduced by the phpng RFCmakes PHP 7 as fast as (or faster than) HHVM. The official benchmarks are impressive: most real world applications running on PHP 5.6 will run at least twice as fast on PHP 7.

Uniform Variable Syntax

The uniform variable syntax is meant to solve a series of inconsistencies when evaluating variable-variable expressions. Consider the following code:

<?php
class Person
{
    public $name = 'Erika';
    public $job = 'Developer Advocate';
}

$person = new Person();
$property = [ 'first' => 'name', 'second' => 'info' ];
echo "\nMy name is " . $person->$property['first'] . "\n\n";

Thanks to the new uniform left-to-right variable syntax, many expressions previously treated as invalid will now become valid. To illustrate this new behavior, consider the following class:

<?php
class Person
{
    public static $company = 'DigitalOcean';
    public function getFriends()
    {
        return [
           'erika' => function () {
                return 'Elephpants and Cats';
           },
           'sammy' => function () {
                return 'Sharks and Penguins';
           }
         ];
    }

    public function getFriendsOf($someone)
    {
         return $this->getFriends()[$someone];
    }

    public static function getNewPerson()
    {
        return new Person();
    }
}

Fatal Error with multiple “default” clauses

This is, again, an edge case and it’s more related to logic errors in your code. There’s no use for multiple default clauses in a switch, but because it never caused any trouble (e.g. no warnings), it can be difficult to detect the mistake. In PHP 5, the last default would be used, but in PHP 7 you will now get a Fatal Error: Switch statements may only contain one default clause.

Return Type Hints

Another important new feature coming with PHP 7 is the ability to define the return type of methods and functions, and it behaves in the same fashion as scalar type hints in regards of coercion and strict mode:

<?php
function a() : bool
{
    return 1;
}
var_dump(a());

How to install ?

To install, first you must add the Webtatic EL yum repository information corresponding to your CentOS/RHEL version to yum:

CentOS/RHEL 7.x:

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

CentOS/RHEL 6.x:

rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm

Now you can install PHP 7.0 (along with an opcode cache) by doing:

yum install php70w php70w-opcache

This will install the mod_php SAPI for PHP, however there are other sapis such as php-fpm (via php70w-fpm package). Read on below for more information about the available SAPIs

If you would like to upgrade php to this version it is recommended that you first check that your system will support the upgrade, e.g. making sure any CPanel-like software can run after the upgrade.

Unless you know what you are doing, it is risky upgrading an existing system. It’s much safer to do this by provisioning a separate server to perform the upgrade as a fresh install instead.

If you know what you are doing, you can upgrade PHP by:

yum install yum-plugin-replace
yum replace php-common --replace-with=php70w-common

It will likely give you a message “WARNING: Unable to resolve all providers …”. This is normal, and you can continue by tying “y“. You will be given a chance to see what packages will be installed and removed before again being given a chance to confirm.

 

Drop a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.