How to Optimize PHP Website to Suit Your Goals
In this article, we go over three simples ways to increase the speed and overall performance of your website, using languages like PHP and JSON.
Join the DZone community and get the full member experience.
Join For FreeWebsite and software developers like contractworks care about the speed and performance of their website. But most of them don’t usually have enough time to properly optimize their websites. Below are easy steps which are time efficient and will improve a website's performance.
PHP Accelerator
PHP accelerator has been emphasized many times on many computers by different people because it is the first tool to use when you want to optimize your website (my favorite are APC and Zend Optimizer Plus) and some of such computer that is suitable for this purpose can be seen on Techspectacle. You should always keep in mind that the default configuration is not the fastest configuration. One feature of these extensions is that they make sure the cache is up to date by checking up on the file timestamp. Code can’t change immediately in a production situation; therefore, you can disable the feature. It will eliminate the slow
fstat()
calls; but an issue is that after every release, you will need to restart all your web servers.
Autoload With the Composer
A Composer is designed to control dependencies but it can also help with the optimization of a website. Even if your code is cached by an accelerator, your website structure can still try to call various functions like “is_readable” and loop through the included path. I had this same problem with Zend Framework 1.12.1. You can fix this problem fast by editing the composer.JSON file.
"autoload": {
"classmap": ["application/", "library/"]
}
Classmap is an array so you can add new paths continuously. Execute the composer to re-establish the autoloader.
$ php composer.phar update
The Autoloader should get much faster because the classmap is going to be cached by the APC.
Serialize Heavy Objects
Creating an instance of an object with a complicated relationship might take a huge amount of time. A perfect example is Zend Framework’s router. This router is a collection of different routes. Each route is an instance of the “Zend_Controller Router_Route”. Creating an instance of a route means executing the route’s constructor (for every instance). Some routes might be storing other routes which add to the execution time of the website. A configuration is normally stored in an INI file or XML which is required to be opened and parsed in the first place.
If the component is sealed and does not have an impact on the environment, you can ignore the execution process and cache the serialized object. When the instance is required, you can get it from a cache and unserialize it. Obviously, the unserialized function does not come without a price. Some developers are of the opinion that it can sometimes be slower than the normal approach. It might also differ across various PHP versions. With the use of a good internet provider service like Century Link Internet, you can simply use the microtime function to measure it:
microtime(true);
In this router example, you can get a 10% improvement just with this one change.
A major improvement in performance on a non-optimized code is not hard to achieve. The key to success is measuring the problem and applying the 80-20 rule, that is, 80% of problems should be in 20% of the code. XDebug profiler is a tool that will instantly help you highlight where the problems are. Always note to create a profile against your production config.
Opinions expressed by DZone contributors are their own.
Comments