Articles
Image pour le titre du contenu

Ce document est aussi disponible en français fr 

Most of the time, PHP applications uses a configuration file. WIth PHP, there are several option to handle them : .ini file, XML files or PHP array.


.ini files are convenient, as they are nicely laid out, and even easier to read with PHP : just use the parse_ini_file function. However, rewriting such file is a little harder.

XML files are also very popular. This is a widely supported format, and many IDE are able to read them. The configuration may be as complex as one need, even though they end up really long. Yet, there is always the PHP 5 SimpleXML extension that will read it fast.

Configuration files written as a PHP array are both flexible and powerful : they also have the fastest analyser : PHP itself! It is not a surprise that so many PHP application use this concept, such as webfwlog

<?php
        $config["db"] = "none";
        $config["wfwl_syslog"] = "/usr/local/bin/wfwl_syslog";
        $config["syslog_dir"] = "/var/log";
        $config["syslog_filespec"] = "messages";
        $config["verbosity_level"] = 2;
        $config["default_data_source"] = "ulogd";
?>

This configurations files are turned into a PHP variable using only the include() function. And best of all, their rewriting to the disk are also versy easy, thanks to the var_export PHP function.

var_export takes a PHP  variable as argument, and produce the PHP code that will rebuild it. See :

<?php
  print var_export(range('a','e');
?>
This will output : 
array (
  0 => 'a',
  1 => 'b',
  2 => 'c',
  3 => 'd',
  4 => 'e',
)

 

You only need to add the rest of the PHP syntax to make a working inclusion file : 

<?php
  file_put_contents('conf.php','<?php $conf = '.var_export(range('a','e')).'; ?>');
?>
Just take a second look at quotes, so that your generated PHP code won't interfere with the current script.

Easy caching
Var_export may be used to set up a fast caching system.

For example, it may be faster to include a PHP script rather than actually querying the database once again. So, after you have fetched the data from the database, it may be one array of rows. Simply use the var_export function to create a PHP representation of the result, for the next script to include.

<?php
function grosse_requete($requete) {
if (filectime('./cache/'.$id.'.inc.php') - time() < 5 * 60) { // cache de 5 minutes
  include('./cache/'.$id.'.inc.php');
  return $resultats;
}
$res = mysqli_query($id, $requete);
if (mysqli_errno($res) != 0) {
  unlink('./cache/'.$id.'.inc.php');
  return false;
}
while($ligne = mysqli_fetch_assoc($res)) {
  $resultat[] = $ligne;
}
$code_php = '<?php $conf = '.var_export($resultat).'; ?>';
file_put_contents('./cache/'.$id.'.inc.php', $code_php);
return $resultats;
}
?>

 

Debugging

Instead of printing too many messages or print_r on screen,

Au lieu d'afficher des résultats trop nombreux à l'écran, vous pouvez aussi les exporter dans un fichier externe, qui sera plus facile à traiter avec PHP.

A mémoriser

  • Var_export() est disponible depuis PHP 4.2.0
  • Var_export() produit le code de définition de la variable. Il n'ajoute aucun élément supplémentaire, ni affectation, ni balises PHP : elle produit du code PHP incomplet
  • Var_export() peut rapidement devenir TRES gourmand en mémoire si le tableau est immense
  • Var_export() est incompatible avec les ressources
  • Var_export() est compatible avec les objets et dispose d'une méthode magique : __set_state
  • Var_export() ne gère pas les références circulaires

 

Fonctions connexes

 

Commentaires

Vous pouvez ajouter votre commentaire!


Vous devez vous connecter pour commenter