Jump to content

Developing a simple application framework (Part #1)

Guest Anonymous

Recommended Posts

Guest Anonymous

It is common practice for web-developers using PHP to utilize some form of framework or API (Application Program Interface). Having experimented with several a while back, I realized that they all suffered from the same basic flaws - trying to be too clever, providing an excess of facilities, being overly complex and often with poor documentation.

To this end, I have developed a set of classes that whilst I won't directly share with you, I will show you the design principals in creating them.

This tutorial (which forthcoming ones will rely on), is intended for users of PHP 5+ with MySQL. It is aimed at people with a reasonable working knowledge of PHP and should in theory work on most systems. You will also require to have reasonable access to a server with all the relevant systems in place - ideally a small locally hosted system (aka your own machine). There are a few topics on the forums here that discuss in detail the preparation of installing a server locally.

To start with, what is this framework, and how can it benefit you?

Quite simple, the framework cuts down the amount of code in your main scripts making them a lot cleaner. It also provides a set of common tasks that can be utilized anywhere within your program and can be easily extended.

Starting off

The first thing to look at is a structure for where your files are going to go. There are two options here, and I will try to make all the code work in either with only one tiny little change to one file.

First, the preferred directory structure:








This is ideal as it provides an extra security layer. The public can only access pages server from the htdocs folder (and of course any sub-folders)

And the slightly less secure directory structure:








Okay, some of you are probably saying ... but I havn't got an htdocs folder ... in which case this tutorial is probably not for you. It actually can be named a variety of things - I've seen htdocs, public_html, www, wwwroot, webroot ... You will just have to find it yourself.

Naming conventions

Now, I've used this system for a number of years without any trouble, the folder names are fairly self-explanatory, however I'll just go through them:

config -- where configuration file(s) are stored with sensitive data (i.e. database username/password etc)

crons -- cron jobs (I'll got into this one in a later tutorial)

images -- I guess you can figure this one out

include -- this is where we store your program's *specific* include files not the base API files

lib -- this is where we keep the main API files which are generic

styles -- all your stylesheets and any images needed by those.


To start with, we need a single file in the htdocs folder which starts up all the intersting stuff. I call mine "prepend.php", and every other file in the htdocs folder will include it.

File: /path/htdocs/prepend.php


/** Idiot check **/

if (!function_exists("version_compare") || version_compare(phpversion(), "5", "<"))
die("<tt>PHP version not supported</tt>");

/** Define root path relative to this file **/

define('ROOT', "..");

/** Import core API **/

require_once(sprintf(dirname(__FILE__) . "/%s/lib/api.php", ROOT));

/** Initialize the main API **/

API::init(__FILE__, ROOT);



Okay, so what's going on here..

First thing - the idiot check. I always check the PHP version - even on my own systems. It's force of habit, although in my case I look for a specific version i.e. 5.2.5+ in my case.

Next we define the ROOT path. This is the only thing that needs to change depending on your directory structure.

For those using the preferred method use "..", and for those on the slightly less secure method, use ".".

Next, we load up the main API (which we've not written yet, but don't worry, it will get done...).

Finally, we run the main API initialization routines. the two parameters here tell the API exactly where all the relevant files can be found (assuming a directory structure like mine).

Okay, having got all that in place... lets just create our first simple "proper" test page...

File: /path/htdocs/index.php



echo "Hello World";



The initial API

Now before we load up the browser and wonder why you don't see anything ... or see a horrible error message, lets just write the very simple API class itself...

File: /path/lib/api.php or /path/htdocs/lib/api.php


class API
private static $_vars = array();

public static function init( $prepend, $root )
	error_reporting(E_ALL | E_STRICT);

	ini_set("html_errors", 0);

	self::$_vars['time']   = microtime(true);
	self::$_vars['htdocs'] = API::path($prepend);
	self::$_vars['root']   = substr(str_replace("//", "/", str_replace("\\", "/", realpath(self::$_vars['htdocs'] . "/" . $root)) . "/"), 0, -1);

public static function htdocs( )
	return self::$_vars['htdocs'];

public static function path( $filename = __FILE__ )
	return substr(str_replace("//", "/", str_replace("\\", "/", dirname(realpath($filename))) . "/"), 0, -1);

public static function root( )
	return self::$_vars['root'];

public static function time( $accurate = false )
	return $accurate ? self::$_vars['time'] : floor(self::$_vars['time']);



Now I'll give you a demo of this in a moment, but initially I suggest you just load up your browser and hopefully you will see the infamous "Hello World".

If you don't ... erm ... sure you followed my instructions, or have I made a mistake? (It does happen, I just don't like to admit to it often anyway).


Lets modify our test script (index.php) and try some of the basic API functions:

File: /path/htdocs/index.php



echo API::htdocs() . "
echo API::path() . "
echo API::root();



You should now get three paths each containing valuable information that we will need in the next part of this tutorial:

htdocs - this is the fully qualified path of where all your public php documents are

path - this is where the API files a are stored

root - this is the site root path and will be different depending on your directory structure.

Notice that all paths use the / as a directory separator. This works on Windows systems perfectly well so I always force the use of / instead of \.

Also notice that all paths do not have a trailing / -- This is intentional in order to make things clearer later on.


Will that's it for part #1 -- hopefully you've got that working... I'll answer some queries for a few days before I start to expand the services of the main API.

Whats next

Reading configuration files, initializing sessions, securing input variables, simple database class, some more nice basic functions... and lots more... ;)

Link to comment
Share on other sites

  • 5 months later...
  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...