Building a PHP Website

Site Configuration: Config File Apr-11, 2005 06:43pm
The first step in building a PHP coded, dynamic content based Website, typically known as a Content Management System (CMS), is to create a configuration process, whereby you, the Administrator, can change how the site works.

This first step is, in it's most simpilist form, a primitive, text based configuration file, also known as an iniitialization file, or INI file.

There is the PHP function:

array parse_ini_file ( string $filename [, bool $process_sections ] )

which is adequate for most purposes. It has some shortcomings though, two of which I found unacceptable for my usage. These are:
  • Reserved words must not be used as keys (eg. null, yes, no, true, and false).
  • Non-alphanumeric characters in values need to be enclosed in double-quotes (").
The other shortcomings, and other features of this function — as well as a thorough discussion of alternatives — can be read at the PHP.NET documentation.

Here is my version of this function:

array configfile ( mixed $file [, array $thisconfig = NULL, bool $translate = TRUE ] )

The basic differences are:
  • Sections are always processed.
  • An array to append to can be passed ($thisconfig).
  • Values can contain any characters including equals (=).
  • $file can be an array of "key = value" entries to process.
  • Constants are not parsed in values.
  • Superglobals are parsed in values (eg. key = {$GLOBALS['value']}).
  • Keys can be translated to lowercase.
(The last is something I added to support legacy INI files I had with uppercase keys; this feature can be useful though.)

The main advantage of a INI file for site configuration as opposed to using a table in MySQL or something is that this way can be coded in only a few minutes. Here is an example. Obviously, this is a "do nothing" example. But, let me try to elaborate later.

; file.ini

sitename = Basic Concept This Way
admin = administrator@bigwigs.com


<?php
$config 
configfile("file.ini");

if (
$config['option'] == 1)
        ...
?>
No Globals Apr-11, 2005 06:43pm
To me, one of the major mistakes of code design is the excessive use of globals. I do not think that the use of the global keyword is somehow bad or that it's use at all means poor design, just excessive use of globals that need to be referenced:

<?php

function start_process() {
        global 
$option$database$dbname$opcode,
        
$query$cookie;

        if (
$option == 1)
                ...
}

?>


That code may not look bad, but I have seen code with dozens of globals referenced in most of the functions throughout hundreds of source files.

The following example shows how to reduce that kind of complexity with the use of a configuration array. One further step eliminates the use of globals entirely.

<?php

$config 
configfile("file,ini");

function 
start_process() {
        global 
$config;
        
        if (
$config['option'] == 1)
                ...
}

?>


In addition, I keep all "configuration" code in one source file (see above), and in that file is this function:

<?php
function config() {
        
$global $config;

        return 
$config;
}
?>


So I then do this:

<?php

function start_process() {

        
$config config();
        if (
$config['option'] == 1)
                ...
}

?>
No Classes Apr-11, 2005 06:43pm
To me, one of the other major mistakes of code design is the excessive use of classes. Classes really only tend to obscure or even obfuscate code by only adding layers of extra syntax.

<?php

if ($handle opendir('.')) {
    while (
false !== ($file readdir($handle))) {
        if (
$file != "." && $file != "..") {
            echo 
"$file\n";
        }
    }
    
closedir($handle);
}

if (
$d dir('.')) {
    while (
false !== ($file $d->read())) {
        if (
$file != "." && $file != "..") {
            echo 
"$file\n";
        }
    }
    
$d->close();
}

// Okay, dir() is a pseudo-class; but it just
// lacks the use of New (eg. $d = new Dir(); ).

?>


Simple example, yes, but it shows that, the real differnce, to the majority of code that uses classes, is the use of a slightly different syntax:

<?php

    $d
->read();
    
$d->rewind();
    
$d->close();

    
// Vs.
    
    
readdir();
    
rewinddir();
    
closedir();

    
// There is no real benefit; just a different syntax. 
    // How about:

    
dir_read();
    
dir_rewind();
    
dir_close();

?>
Include Code Jul-13, 2010 08:54pm
While starting the code I kept adding include files. Well, lazy programmer that I am, I came up with a way to automatically include any file placed in an "include file directory". I new include file? Just put it in the include dir:

<?php

$GLOBALS
['INCLUDEPATH'] = "_inc";

include 
"error.php";

$incs _includes($GLOBALS['INCLUDEPATH']);
foreach (
$incs as $inc)
        include_once(
$inc);

...

function 
_includes($dir) {

        
$incs = array();
        
$d opendir($dir);
        while ((
$_ readdir($d)) != "") {
                if (
preg_match("/.php$/","$dir/$_"))
                        
$incs[] = "$dir/$_";
        }
        
closedir($d);

        return 
$incs;
}

?>