📜 ⬆️ ⬇️

PHP is alive. PHP 7 in practice

Recently, Avito PHP projects switched to PHP 7.1. On this occasion, we decided to remember how the transition to PHP 7.0 took place between us and our colleagues from OLX. Cases of bygone days, but there are beautiful graphics that you want to show the world.


The first part of the story is based on the article PHP's not dead! PHP7 in practice , which was written by our colleague from OLX Łukasz Szymański (Lukasz Szymański): OLX transition to PHP 7. In the second part - Avito transition experience to PHP 7.0 and PHP 7.1: process, difficulties, results with graphs.




Part 1. PHP 7 in OLX


OLX Europe manages ten sites, the largest of which is OLX.pl. All our sites should work as efficiently as possible, so migration to PHP 7 has become our main priority.


In this post, we’ll tell you what problems we had to face and what we managed to get with the transition to PHP 7. The transition was discussed at the PHPers Summit 2016 conference.


Slides

Transition


Contrary to our concerns, the migration went smoothly. Except for the standard list of necessary changes from the official documentation , it was necessary to make only some changes related to our architecture.


It is worth mentioning that ten of our sites operate in different countries. And we roll out the changes consistently: one site after another. This approach is especially important to apply with major changes.


We started updating the version from the smallest site and moved on to an ever larger one, looking for tests to be successful. This allowed to monitor the occurrence of unexpected problems and reduced potential damage.


Memcache


Refusal to support Memcache in PHP 7 pushed us to switch to Memcached. I had to support two versions of the site: PHP 5 + Memcache and PHP 7 + Memcached.


To solve the problem used a simple wrapper. It selects the appropriate PHP module to connect to the cache, based on the information about the server on which the code is executed.


<?php $factory = new CacheWrapperFactory(); $factory->createServer( extension_loaded('memcache') ? 'memcache' : 'memcached', $uri ); 

However, the adventures with Memcached are not over. It turned out that objects serialized with Memcache cannot be deserialized with Memcached.


But the cache is the cache and that it can be easily removed. Therefore, we simply deleted the old problem objects from the cache, and they were recreated using the new module.



APC, APCu and OPCache


A little about the terms.


APC (Alternative PHP Cache) is a bytecode cache and user data.
APCu (APC User Cache) - user data cache only.
OPCache - bytecode cache only.


There is no APC in PHP 7, we had to take both APCu and OPCache. Previously, we used the APC API in many critical parts of our cache management framework, so we screwed the apcu-bc API - compatible API module to APCu .


results


Below are the results of the largest of our sites, OLX.pl.


Apache CPU


Our web servers (Apache) revolve on 20 physical machines with 32 processor cores each. Peak CPU consumption as a result of migration decreased from 50% to 20%.



LA to Apache


Similarly, Load Average on web servers has decreased.



The numbers speak for themselves: reducing the load, saving resources, increasing efficiency. If your project managers or clients do not give enough time to migrate, these charts will surely be able to convince them.


PHP 7 efficiency reasons


Such amazing results have been achieved thanks to optimization in three main areas.


Fewer memory allocations


PHP 5 consumed 20% of its CPU time only for memory allocation operations. The developers of the language paid attention to this, reduced the number of memory allocation operations, which significantly reduced the CPU consumption.


Less memory consumption



This sketch shows the path that the processor needs to go through to access RAM (RAM), with time for each step. As you can see, the segment to RAM is the longest. PHP developers have reduced memory consumption by speeding up application response.


Less intermediate pointers



The final reason for improving PHP 7 efficiency is to reduce the number of intermediate pointers. To do this, PHP developers have eliminated a lot of pointers that refer to other pointers; instead, they made the pointers refer directly to the requested entities.


Code


In addition to improving efficiency, PHP 7 has a small revolution in code structure.


Scalars in the function declaration


Before PHP 7, only an object, an interface, an array, or a callback function (callable) could act as the argument type of the function. Take for example the following code.



Obviously, the use of such methods poses many problems if you are not sure of the input arguments being correct.


In addition to adding type checks to each method, you can use the ValueObject construct from subject-oriented programming (DDD).



PHP 7 allows you to simply specify scalars, such as string, int, float, bool. You can also specify the type of return value.



Strict mode


But simply adding the above constructs to the code may not give the expected results. That's because PHP 7 by default works in a coercive mode, in which the usual type conversions take place. If you do not forcefully turn on the strict mode, you can rewrite the above method as follows.



Unfortunately, even adding the declare clause (strict_types = 1) to the PHPisNotDead.php file does not include the strict mode. The explanation is in the example below. The comments indicate the return values.



Why it happens? Strong typing in the methods of the PHPisNotDead class is enabled only for return values.



If you need to enable strong typing for arguments as well, you will have to add the declaration strict_types and in all files in which the methods of the class are called.



More information about specifying types and its impact on program execution can be found in the documentation . Reading this documentation will save you surprises when executing your code.


Future


If even now you still have not decided to switch to PHP 7, take a look at the list of supported versions of PHP . Version 5.6 is no longer actively supported, and at the end of 2018 even the fixes for critical vulnerabilities will stop being released. Active support continues for versions 7.0 and above.



Stay tuned for PHP news and new plans. The most interesting posts: friendly classes , generic types and functions . Find other language development suggestions in PHP RFC.


Results


PHP 7 is impressive: in addition to improving efficiency, it helps developers write better code. I sketched out a small tutorial that will help you decide on the transition.


- When should I upgrade to PHP 7?
- Right now.


Part 2. PHP 7 in Avito


The transition process to PHP 7.0


We, like OLX, made the transfer of our services to PHP 7 gradually. At first, they translated small detached services, tested them, corrected errors that occurred. Then they moved to a consistent update of the admin site servers, and then rolled out the remaining services and the site to PHP 7.


Transition difficulties


We read the list back for incompatible changes . However, this did not save from all troubles.


In the old code there was a class called String. PHP 7 produced the error “Cannot use 'String' as class name as it is reserved”. Class renamed. Pay attention to the list of reserved words .


In PHP 7, the cache file format for the WSDL schema in SoapClient has changed. You can configure to save the cache in different directories depending on the version of PHP or completely clear the cache before changing the version of the interpreter.


The mongo extension, which we actively used, was no longer supported in the new PHP. Instead, we began to use the official MongoDB PHP Library . We walked through the whole code and replaced MongoCollection :: insert () with MongoDB \ Collection :: insertOne (), MongoCollection :: remove () with MongoDB \ Collection :: deleteMany () and further along the list . We began to use classes for working with BSON from the new MongoDB driver , for example, MongoDate instead of MongoDB \ BSON \ UTCDateTime.


Result


Admin feel better.




The backend of the site is also satisfied.




Upgrade to PHP 7.1


In PHP 7.1, there are some very nice innovations: the void type for return values, iterable, the ability to return null for typed return values, the scope of constants, and so on. In addition, the period of active support for PHP 7.0 ends at the end of this year. We decided to upgrade to PHP 7.1.


Surprises


When upgrading out of the blue, a problem arose. The php-memcached package for 7.1 pulled the php-igbinary package along. When PHP 7.1 was installed on one of the combat servers, errors from the rest of the servers started to appear, in which the word “igbinary” appeared.


An old friend of Memcache, again the differences of serialization, but a little under a different sauce. It turned out that the php-memcached module by default uses the first available serializer from the list: igbinary (in a separate module), msgpack (in a separate module), php (does not require a separate module, is always available). And that server, on which we set 7.1 with igbinary, started recording data into a memkey, serialized by igbinary. And on the other servers there was no support for this serializer, and they could not read the data recorded by the server with the updated PHP. Localized the problem, installed igbinary on all other servers, errors stopped.


Afterword


PHP developers have taken a good course. They add useful tools, get rid of the shortcomings associated with the heritage of the language, seriously think about performance.


We have already talked about the Avito transition to the service architecture ( one , two ). This architecture allows you to write in any languages, and we most often write new services in Go or Python. However, there is no talk of abandoning PHP: the main logic of the site (monolith) is still in PHP, and the team knows how to work with it. New versions of the interpreter allow you to make the code better, and its execution is faster.


Share your experience in switching to PHP 7 and higher, we will be happy to discuss the discoveries and rakes that you encountered along the way.


')

Source: https://habr.com/ru/post/338140/


All Articles