Version 2.3.1
Last updated 14th November 2005


This is a port of the Application Constants plugin for Mach-II Coldfusion by Martin Laine, there is one difference between the two, which is outlined below.

This plugin allows you to have a different set of application constants for each server running your Mach-II application. This can be useful if you have different settings for your different environments, for example debugging turned on within the development environment.

Note: this plugin requires the PHP 5 version of Mach-II, this is mainly because of the usage of simple_xml. It could be re-worked to run on the PHP 4 version; however I don't have anyway to test that.

Differences from the Coldfusion version:

The Coldfusion version also registers a preEvent() method, which places the AppConstants structure into the event-args for each event. This was done due to the hacky nature of getting at Mach-II properties prior to the 1.0.10 Coldfusion version. I never thought this was nice, and we even hacked the Coldfusion version we were using at work to put each constant directly into the properties.

As the PHP version of Mach-II has being built with the BaseComponent in place since it's public release, which allows direct access to Mach-II properties from anywhere where you're likely to need them, I decided not to include this fudge, and Martin had already added the ability to set properties directly in the latest version.

I'm sure the preEvent() method is is only still in place in the Coldfusion version to provided backwards compatability for current users.


Place AppConstants.php in the MachII plugins folder.

Mach-II configuration file:

  1. <plugin name="appConstants" type="MachII.plugins.appConstants">
  2.     <parameters>
  3.         <parameter name="constantsFile" value="path/to/constants/file.xml" />
  4.     </parameters>
  5. </plugin>

The constantsFile optional parameter should be a path to your constants XML file relative to your application root (set in the properties section of your configuration file). It defaults to “/config/constants.xml”.

The plugin will attempt to resolve the full path to the file by using testing the path combined with each of the values set in include_path ini variable.

Constants File:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <constants>
  3.     <common>
  4.         <constant name="constant_name_1" value="constant_value_1" />
  5.         ...
  6.         <property name="property_name_1" value="property_value_1" />
  7.         ...
  8.     </common>
  9.     <server name="your_dev_server_name">
  10.         <constant name="constant_name_1" value="constant_value_1" />
  11.         ...
  12.         <property name="property_name_1" value="property_value_1" />
  13.         ...
  14.     </server>
  15.     <server name="your_testing_server_name">
  16.         <constant name="constant_name_1" value="constant_value_1" />
  17.         ...
  18.         <property name="property_name_1" value="property_value_1" />
  19.         ...
  20.     </server>
  21.     <server name="your_live_server_name">
  22.         <constant name="constant_name_1" value="constant_value_1" />
  23.         ...
  24.         <property name="property_name_1" value="property_value_1" />
  25.         ...
  26.     </server>
  27. </constants>

All constants & properties within the common section will be available no matter which server the application is running on. Constants & properties set within the common section are over-ridden by those with the same name in the individual server sections.

You will have access to the constants within the server section of the constants file that corresponds to the server currently running your application on. The server name can be an individual server or a comma separated list, useful if several domains point to the same application or in a development environment where each developer works on a local copy etc.

All constants are placed in an array, with named indices, named appConstants that you can access as a Mach-II property, this is handy if you are worried about conflicts with property names within the Mach-II configuration file.

All properties are placed directly in Mach-II properties, these will overwrite any existing Mach-II properties with the same name.

Note: You can use this plugin for a single server setup. Place all your constants in the common block and don't bother with any server blocks.

Next Steps


Leave a Tip

If you find this code useful you can leave a donation towards the continued development & support.

Comments are disabled on this post.