Eli : Memcached, MySQL, Highcharts

PHP - PHP - Eli : Memcached, MySQL

PHP - PHP - Eli : Memcached, MySQL

PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.

Monday, September 20 2010

Tricks to speed up Eclipse Helios, PDT, Eclipse Juno, Eclipse Indigo or Eclipse Kepler

logo-eclipse-helios.png

Eclipse can be slow, very slow, here's some quick steps to increase Eclipse speed.

Download the latest version of Eclipse and Java

You must upgrade to the latest version in the official Helios download page
A note of the latest version of PDT that make interesting changes, check this page to know more about them.
Upgrading Java Virtual Machine is also essential.

Eclipse PDT Configuration

Add more RAM to Eclipse

Open configuration file eclipse.ini in your Eclipse installation folder, then add more memory, and restart.

--launcher.XXMaxPermSize
512M
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
512m
--launcher.defaultAction
openFile
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xms256m
-Xmx512m

Maybe buying more RAM for your computer can help if you have a lot of applications launched at the same time.

Disable Eclipse validators

By default, all code validators are running, go to Windows > Preferences > Validation and uncheck validators you don't need.

Disable Plugins you won't use and startup builds.

Go to Windows > Preferences > General > Startup and Shutdown and uncheck plugins you don't need.
Don't forget to disable startup automatic builds.

Disable Eclipse PDT automatic build

Go to Windows > Preferences > General > Workspace and uncheck build automatically.

Eclipse Helios use

Close any open PHP projects not used

Open project are refreshed, build, and validated even if you don't use them, right-click on Project > Close project, this will free some memory.

Make your projects smaller and create multiple small PHP projects instead

Having a whole big project is not a good idea, divide them into smaller project, and close the one you don't use or you don't need.

Avoid project on network disk

Network disk latency and slow access can really slow your eclipse, especially if you don't have disabled builds, use a version control system and checkout it on your local disk.

Defragment your local disk

Simple isn't it.

Feel free to comment or add your own tips.


Monday, June 28 2010

Handle configuration in PHP with var_export()

Handling configuration file with PHP

During development i searched an easy way to handle configuration files, either via manual editing or via interface.
At first i was using parse_ini_file() but it becomes quickly clear that i was not made to handle complex data structure.

After some readings i finally end up using var_export() to handle my configuration files.
Note that var_export() also work with objects, but you need first to implement _set_state() which can be quickly painful, especially if your object link to other objects as everyone must have a _set_state() method, so in this example i will stay only with array.

The function var_export()

The goal of var_export() is to return a parsable string representation of a variable, in clear, it return valid PHP code.

mixed var_export  (  mixed $expression  [,  bool $return = false  ] )

It output immediately $expression in PHP code if $return is set to false, or return it as a string if $return is set to true.

What we want is to write our configuration array into a PHP file, so we will use it with $return at true, otherwise you can use ob_start() and ob_get_contents().

Here our configuration file example :

<?php
return array (
  'connection_timeout' => '3',
  'storage' => 'SQLite',
  'clusters' => 
  array (
    'production_cluster' => 
    array (
      0 => '127.0.1.1:11211',
      1 => '127.0.1.2:11211',
      2 => '127.0.1.3:11211',
    ),
    'integration_cluster' => 
    array (
      0 => '128.0.1.1:11211',
      1 => '128.0.1.2:11211',
      2 => '128.0.1.2:11211',
    ),
  ),
);

To include it and use it, we only have to require it, it will use PHP include_path to do so.

/**
 * Load and return configuration file of path $path
 *
 * @param string $path Configuration file path
 *
 * @return array
 */
function load($path) 
{
    return require $path;
}
 
# Now we can use our configuration as an array ...
$myConf = load('Config/file.php');

We can go even further by adding an environment variable (dev, prod, ...) to it to handle and load the right configuration for the right place.

After load, we can implement all things that are needed to use and edit this configuration.
To save it as a PHP file, we must add a php open brace, the return instruction and a ; after the array for this instruction.
All the PHP code returned will be well formatted by var_export() to keep reading easy.

/**
 * Save configuration array $array to path $path
 * Return true if successful, false otherwise
 *
 * @param string $path Configuration file path
 * @param string $array Configuration array
 *
 * @return boolean
 */
function save($path, $array) 
{
    $content = '<?php' . PHP_EOL . 'return ' . var_export($array, true) . ';';
    return is_numeric(file_put_contents($path, $content));
}
 
# Saving configuration
save('Config/file.php', $myModifiedConf);

Now we can load and save one configuration file, how about doing some array_merge() to load base configuration file, and an user redefined configuration file.

Why not using serialize() or json_encode() ?

Both serialize() and unserialize are faster than json_encode() / json_decode() and json functions are not made for this.
Even if it could be a good option, serialized content look likes a big line with a lot of number to most developer and manual editing is a bit tricky, since you must count length of each items, and modify the s:, a: in consequence.

a:3:{s:18:"connection_timeout";s:1:"3";s:7:"storage";s:6:"SQLite";s:8:"clusters";a:2:{s:18:"production_cluster";a:3:{i:0;s:15:"127.0.1.1:11211";i:1;s:15:"127.0.1.2:11211";i:2;s:15:"127.0.1.3:11211";}s:19:"integration_cluster";a:3:{i:0;s:15:"128.0.1.1:11211";i:1;s:15:"128.0.1.2:11211";i:2;s:15:"128.0.1.2:11211";}}}

So I will stick with var_export() for the moment as it keep the configuration code clean and easy editable.


Tuesday, April 20 2010

Handling fatal error in PHP with register_shutdown_function

PHP 5.2.0 bring us a new function : error_get_last()

It return an associative array describing the last error with keys "type", "message", "file" and "line" about the last error that occurred.

We can now use it with register_shutdown_function to handle fatal error, log, redirect, html page, instead of the infamous white page.

/**
 * Handling fatal error
 *
 * @return void
 */
function fatalErrorHandler()
{
    # Getting last error
    $error = error_get_last();
 
    # Checking if last error is a fatal error 
    if(($error['type'] === E_ERROR) || ($error['type'] === E_USER_ERROR))
    {
         # Here we handle the error, displaying HTML, logging, ...
         echo 'Sorry, a serious error has occured in ' . $error['file'];
    }
}
 
# Registering shutdown function
register_shutdown_function('fatalErrorHandler');

Resources :

  • http://www.php.net/manual/en/function.error-get-last.php
  • http://php.net/manual/en/errorfunc.constants.php

page 2 of 2 -