Android,iOS,Gadgets,Reviews Everything About Technology

- Advertisement -

A few simple ways to increase the performance of PHP



To make a site more productive, you need to connect a lot of additional layers. From creating a reverse proxy cache with Varnish to configuring the load balancer group. And for this there are many well-documented options.

- Advertisement -

But, what if you are just starting out? What if you have a small application? What can you do right now to immediately feel the difference in performance?

This article discusses simple ways to increase the performance of PHP, you can use them as standard practices on your team or apply to an already existing application.

The very first thing is an upgrade to PHP 5.4

At the time of this writing, the latest version of PHP was 5.5.5. if you have appropriate control over the server, you should consider using this version. While 5.5 introduces many new concepts and security updates, it is still poorly tested and is not widely used on product servers. If php 5.5.5 is not an option, then the minimum version should be PHP 5.4. PHP 5.3 is not only nearly 4 years old, but it’s also slower and uses more memory than PHP 5.4.

Working with Drupal? PHP 5.4 is 7% faster and uses twice less memory than PHP 5.3.

Auto Backup

Startup is the connection of files with classes that are used in your application, without having to manually refer to the paths to these files. We can use Composer to achieve this result. Composer is designed to manage dependencies and has been reviewed in previous articles. For startup, you can simply add the location of classes to a file composer.json. The only configuration option that allows you to do file mapping is the key in the JSON array.

For example, if you have classes in a directory, srcyou need to define it like this:

    "autoload": {
        "psr-0": { "": "src/" }

If your classes have spread to folders resourcesor library, the format is slightly different:

    "autoload": {
        "classmap": ["resources/", "library/", "Something.php"]

However, you need to know that this autoload reduces the need to use requireor include_once, the performance can suffer if you try to download a large number of files. But you can, soften this situation if you use accelerator or caching via opcode . An alternative approach to autoloading files, which also costs to learn is the component Classloaderin Symfony2 framework, the technique is described in this article.

Reducing memory usage in code

There are certain techniques when using operators, loops, or assigning a value to a variable that use less memory and can possibly make the entire application more productive. If necessary, you can make the properties of the object public in order to avoid unnecessary methods for accessing this variable (setters or getters), so less memory will be used when executing the script (but making properties public is bad practice in terms of the design architecture):

~~~ {.php} <? php

- Advertisement -

class A {public $ foo; public function setFoo ($ bar) {$ this-> foo = $ bar; } public function getFoo () {return $ this-> foo; }}

$ bat = new A ();

// Slower than $ bat-> setFoo (‘baz’); $ baz = $ bat-> getFoo ();

// Faster than $ bat-> foo = ‘baz’; $ baz = $ bat-> foo; // Determine the size of the iteration before executing the loop:

// Slower for ($ i = 0; $ i <count ($ j); $ i ++)

// Faster $ count = count ($ j); for ($ i = 0; $ i <$ count; $ i ++)

// Use the isset () language construct before the operation:

// check the presence and type of the variable if (isset ($ foo) && is_array ($ foo)) {return $ bar; }

These techniques are part of Google's recommendations and documented in benchmarks. They obviously depend on the size of your data values ​​and the complexity of the code, but getting used to the good in the beginning gives less headache in the future

### Profiling
An objective measurement of how well your masterpiece works provides valuable information and reduces subjective distractions. Profiling the code is very simple, but it requires a separate extension in PHP. You can use the extension ** XDebug extension ** or ** Advanced PHP Debugger (APD) ** to profiler your code. Just update `php.ini` or add an INI file with the appropriate configuration parameters:
For example, the XDebug configuration for OSX:

Find out how much time is spent on processes or functions, and if necessary, customize them in an appropriate way.

For more reliable profiling and monitoring, you can also install tools from AppDynamics that, when installed as a server daemon and php extension, allow you to conduct in-depth analysis of your complex and large applications. This is an excellent option for optimizing old code.

OpCode Cache

Last, but not least, is the code execution in memory. This is more productive, because this approach reduces the need to read the code from the disk and compile it again. As of PHP 5.5 OpCache is an open source caching system from Zend, which is enabled by default (another reason for updating). For earlier versions, you can easily install ZendOpCache via PECL.

ZendOpCache configuration example on OSX:

~~~ {.ini} [opcache] zend_extension = “/ usr / local / Cellar / php54-opcache / 7.0.2 /”
zend_optimizerplus.memory_consumption = 128
zend_optimizerplus.interned_strings_buffer = 8
zend_optimizerplus.max_accelerated_files = 4000
zend_optimizerplus.revalidate_freq = 60
zend_optimizerplus.fast_shutdown = 1
zend_optimizerplus.enable_cli = 1
zend_optimizerplus.enable_file_override = 1
apc.cache_by_default = false

### Conclusion
Let's hope that the above methods will give you a starting point for creating faster PHP applications. These techniques are simple enough to implement them right now. Do not wait!

- Advertisement -