PHPBuilder - Using PHP Configuration Patterns Properly

RSS Twitter
Articles Application Architecture

Using PHP Configuration Patterns Properly

by: Kaushik Pal
April 3, 2014


PHP is a cross platform language. It is a server based application so we must think about the configuration settings of the PHP software. There are various ways of creating configurable PHP applications. The configuration flexibility comes as a built in feature in PHP. But we must understand the requirement clearly before making an application configurable.


This article explores different PHP configuration patterns and their implementation.



There are several ways to make a PHP application configurable. There are also ideal configuration points which are industry standard. These points will determine whether a PHP application is too configurable or too closed. If a PHP application is for a client installation then it should be configurable enough to fit custom requirements. There are different techniques available for storing configuration settings and the options are mentioned below.


1. INI file

2. PHP file

3. Text file

4. XML file

5. Database


Every choice has its advantages and disadvantages. All of these choices will be discussed to help you figure out which technique is correct for your application.


The INI File Pattern

PHP has built in support for configuration files. The task is performed by the initialization (INI) file mechanism, which is known as php.ini file. This file defines constants, database connection parameters, session related values, etc. But the custom configuration parameters can also be included in php.ini file as per your application requirement. Let us check a simple example.


Let us add a simple line in php.ini file.


myappworkdir = phpbuilder 


Listing 1: sample.php showing entry in php.ini file


function get_work_directory()
  $v = get_cfg_var( "myappworkdir " );
  return ( $v == null ) ? "workdir" : $v;
echo( get_work_directory()."\n" );


Now let's run this on command prompt. It will display the value as shown below.


% php sample.php


We must remember that using the INI file for configuration of entries is not recommended for your application. The main reason is that the INI file can be read easily but writing into it in a secure manner is almost impossible. As a consequence, the INI file is suitable for read only operations. If your application needs both read and write operations, then the INI file as a configuration pattern should be avoided. The second reason is that the php.ini file is shared among all the applications running on the server, so application specific entries are not recommended.


PHP Script File Pattern

PHP script can also be used to store the configuration items. This is nothing but a PHP file which holds the configuration parameters. The first step is to create a PHP file and then add the constants as per the application requirement. These constants can be accessed directly from other files.


The following sample shows one script file which holds the constants and the second PHP file is accessing the values.


Listing 2: Sample showing configuration entries (configscript.php)


# Working directory location
$WORK_DIRECTORY = "workdir";


Listing 3: .Sample showing accessing configuration values (test.php)


require_once 'configscript.php';
function get_work_directory()
echo( get_work_directory()."\n" );


Now let us discuss the advantages of this design pattern. First, the file is newly created so it is just a blank file where you can add the parameters. You can also put the PHP script file in the same file as the root. The second advantage is that the file can be edited by using any standard editor.


Along with the advantages there are also some disadvantages. This pattern is also a read-only pattern, like INI file discussed in the previous section. So the read operation is very easy but write is almost impossible to perform.


Text File Pattern

Our previous two patterns were suitable for read-only operations. But if the application needs to read and write the configuration items, then we should look for other options. Next we will discuss the text configuration pattern.


Let us check one example of implementing text file pattern. The following is a simple (txt) file containing configuration entries.


Listing 4: Sample showing text file entries (config.txt)


# Application's text configuration file
Title=My App in phpbuilder


The next part is a self defined configuration class to access the text configuration file entries.


Listing 5: Sample showing retrieval of configuration values (test.php)


class TextConfiguration
  private $configFileText = 'config.txt';
  private $itemslist = array();
  function __construct() { $this->parseFile(); }
  function __get($id) { return $this->itemslist[ $id ]; }
  function parseFile()
    $fl = fopen( $this->configFileText, 'r' );
    while( $l = fgets( $fl ) )
      if ( preg_match( '/^#/', $l ) == false )
        preg_match( '/^(.*?)=(.*?)$/', $l, $found );
        $this->itemslist[ $found[1] ] = $found[2];
    fclose( $fl );
$t = new TextConfiguration();
echo( $t->WorkDirectory."\n" );


In this class, we are creating a configuration class object. After this the constructor reads the content of the text configuration file and then a local variable is populated with the parsed data. In the next step the script searches the 'WorkDirectory'and finally it displays the value.


If the PHP file is run on the command prompt, then the following result will be displayed.


% php test.php 


Similarly, the write operation can also be performed on the same text configuration file. We need to add some more methods in the custom configuration class to save the data in the text file. Although the coding part is a bit more complicated in this approach, it supports both the read and write operation.


XML File Pattern

An XML file is another option that can be used as a PHP configuration pattern. XML files are also widely used as configuration files in various applications, so it is an industry standard. In addition to this, XML files are tag based and there are many standard editors available.


Let us first check the configuration file which holds the parameter values.


Listing 6: Sample showing xml configurations file (config.xml)


<?xml version="1.0"?>
  <Title>My app in phpbuilder</Title>


The following is an updated configuration class for accessing data from xml configuration file.


Listing 7: Sample showing usages of xml configuration file (testxml.php)


class XMLConfiguration
  private $configFileXML = 'config.xml';
  private $itemslist = array();
  function __construct() { $this->parse(); }
  function __get($id) { return $this->itemslist[ $id ]; }
  function parseXML()
    $doc = new DOMDocument();
    $doc->load( $this->configFileXML );
    $cn = $doc->getElementsByTagName( "config" );
    $nodes = $cn->item(0)->getElementsByTagName( "*" );
    foreach( $nodes as $node )
      $this->itemslist[ $node->nodeName ] = $node->nodeValue;
$c = new XMLConfiguration();
echo( $c->WorkDirectory."\n" );


The XML configuration pattern is much cleaner and easier to implement. If the code above is run on the command prompt then it will display the same output. The sample shows the read operation only. The write operation can also be performed by adding some more methods.


Database Pattern

Now we will discuss the last PHP configuration pattern that is based on a database. The database will hold the configuration values. And a custom class has to be designed as above to access the database schema and retrieve the values.


The database based configuration pattern is basically a hybrid pattern. It is a mixture of text and database pattern. The configuration class is designed to read the initial values from a text file and then access the database to update the related entries. But this approach is flexible enough to use in various applications.



In this article, we have touched all the aspects of PHP configuration patterns and explained that the configuration part of a PHP application must be considered during the design phase. PHP applications can be of various types, depending upon the functionality, so the selection of a particular configuration pattern is a key factor for success. I hope you have understood the basic concepts of PHP configuration patterns and their implementation in real world applications.



About the Author

Kaushik Pal is a technical architect with 15 years of experience in enterprise application and product development. He has expertise in web technologies, architecture/design, java/j2ee, Open source and big data technologies. You can find more of his work at and you can email him here.

Comment and Contribute

Your comment has been submitted and is pending approval.

Kaushik Pal



(Maximum characters: 1200). You have characters left.