Magento and PHP 7

Magento is one of the biggest and most popular e-commerce platforms written in PHP. The very first Magento release, known as ‘Bento’, is dated to March 31, 2008. For that time the actual PHP version was 5.2. Nowadays we are happy users of Magento 1.9 that supports PHP versions up to 5.5.x (officially announced), but the platform also smoothly runs on PHP v5.6. Recently we had good news about Magento 2.0 release and that fact is really great. From the opposite side, the first version of Magento will lose official support quite soon but, we are sure, there are many merchants who will stick with Magento 1.x for the next couple of years because of different reasons.

As developers we need to proceed with finding the ways to improve the platform, including possibilities to make it compatible with the newest solutions from the world of web technologies. Today it is about a time to start our findings since PHP v7 is coming and Magento 1.9 is not compatible with the newest version out of the box, but fortunately, it’s quite easy to fix.

As you can see, the latest PHP version was v5.6.x and the next major release is 7.0. The question is what happened with the version six? After implementing a pack of major features, planned only for the seventh version, PHP core contributors decided to skip version 6.0. Also, one of the very important reasons was “As a special non serious bonus, 7 is perceived as a lucky number in both the Western world and Chinese culture. A little bit of luck never hurt anybody” (https://wiki.php.net/rfc/php6). Honestly, there are many reasons to switch to the latest PHP version but the most exciting of them is “PHP 7 is 2X faster”. Ok, let’s see.

We hope, by the time you are reading this article, PHP 7 is officially released and there are no difficulties with it’s installation. So, let’s assume you have already installed the latest version. If not, you can find tons of articles how to install it on different systems using your favourite search engine. Well then, on the first attempt to load a Magento page using PHP 7.0 you probably will get the following error:

Fatal error: Uncaught Error: Function name must be a string in app/code/core/Mage/Core/Model/Layout.php:555

It happens because in PHP 7 you need to clarify that you are going to call the `$callback` variable as a method (function). So, the original line of the code looks like the following (file app/code/core/Mage/Core/Model/Layout.php):

$out .= $this->getBlock($callback[0])->$callback[1]();

In order to make it work on the latest PHP version we need to replace this piece of code by this one:

$out .= $this->getBlock($callback[0])->{$callback[1]}();

Please note that you should not edit the mentioned file directly. The most preferred way is to create a separate extension and override “Mage_Core_Model_Layout” by your own model and make the changes there. You can find information about a custom extension development and components overriding in our previous articles. Or you can create a new directory:

app/code/local/Mage/Core/Model/

Then copy “Layout.php” file there and make all necessary changes inside of the new copy. Do not forget to clean the cache after all modifications.

After these adjustments you should be able to load your store’s pages successfully.
If you try to run Magento on the PHP version prior to 7.0 RC3 you will probably face with inability to enter the admin panel. The fix for Magento is tricky in that case, so, it’s much faster to upgrade PHP version to the latest one – it will save your time.

Finally (the most interesting part) we’ve decided to compare two clean Magento installations with official sample data populated on PHP 5.6 (OpCache enabled) and PHP 7.0 (OpCache enabled). The benchmark is based on 20 simultaneous visits for few category pages, one configurable product info page, Magento 2 search page and about us page. The results are more than impressive:

PHP 5.6

benchmark php56

PHP 7.0

Benchmark php70

As you can see, PHP 7 is really fast and definitely worth a try in case of Magento. If you have faced with some other issues and have interesting workarounds we will be glad to discuss them in the comments below. Thank you.

You may also want to read: