TechAlpine – The Technology world

What are the PHP configuration patterns?

PHP Configuration Patterns

PHP Configuration Patterns

Overview: 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 will discuss different PHP configuration patterns and their implementation.

Introduction:

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 some 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. Now all of these choices will be discovered to observe which technique is accurate for the working 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 me add a simple line in php.ini file.

myappworkdir = phpbuilder

Listing1: sample.php showing entry in php.ini file

<?php

function get_work_directory()

{

$v = get_cfg_var( “myappworkdir ” );

return ( $v == null ) ? “workdir” : $v;

}

echo( get_work_directory().”\n” );

?>

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

% php sample.php

phpbuilder

%

We must remember that INI file for configuration entries is not recommended for your applications. The main reason is that the INI file can be read easily but writing into it in a secure way is almost impossible. So 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 must 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)

<?php

# Working directory location

#

$WORK_DIRECTORY = “workdir”;

?>

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

<?php

require_once ‘configscript.php’;

function get_work_directory()

{

global $WORK_DIRECTORY;

return $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 some disadvantages also. 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 difficult 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. Here I will discuss about the text configuration pattern.

Let us check one example implementing text file pattern.

Following is the first text file having configuration entries. It is a simple (txt) file having configuration entries.

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

# Application’s text configuration file

Title=My App in phpbuilder

WorkDirectory=workdir

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)

<?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 search 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

workdir

%

In the similar way 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 in this approach but it supports both the read and write operation.

XML File Pattern: In the previous section I have discussed the text file pattern with support for read and write operations.XML file is another option which can be used as a PHP configuration pattern. XML files are also widely used as configuration file in various applications so it is an industry standard. In addition to this, XML files are tag based and there are lots of standard editors available for editing XML files.

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

Listing6: Sample showing xml configurations file (config.xml)

<?xml version=”1.0″?>

<config>

<Title>My app in phpbuilder</Title>

<WorkDirectory>workdir</WorkDirectory>

</config>

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

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

<?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 above code is run on the command prompt then it will display the same output as above. The above 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 which is based on 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.

Conclusion: In this article we have touched all the aspects of PHP configuration patterns. We have also understood that the configuration part of a PHP application must be considered during 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 the success. I hope you have understood the basic concepts of PHP configuration patterns and their implementation in real world applications.

Leave a Reply

Your email address will not be published. Required fields are marked *


6 − = 3

TechAlpine Books
-----------------------------------------------------------