What is dependency injection 

A simple definition is:

Dependency Injection is where components are given their dependencies through their constructors, methods, or directly into fields.

For example a class constructor creates an instance of an object. This class is now dependant on that object. If however this object is passed into the constructor this dependancy is decoupled.

For dependancy injection to work correctly each class requires an interface. This will ensure if the object is switched it will still present the same methods, it doesn’t however need to know what these methods do.

Unit testing as one of the main uses for dependancy injection. It allows objects which perform complex data processing tasks to be replaced with an object which provides mock data for testing. The class being tested doesn’t care how this data is created as long as it’s presented in the same format.

Another example is an object provided SQL database access methods. Using dependancy injection we can switch this to another object with MySQL access methods. As long as both objects expose the same methods the objects using them won’t care which one it is.

PHP Class Variables and Methods

The class variable is declared outside of all other methods. To reference the variable use the following syntax.


Below is a brief example on how this would be used. The class variable is defined outside any functions, the variable is then set inside the constructor.

class testClass
protected testVariable
public function __construct()
$this->testVariable = 0;

This same syntax is the same for referencing functions within the same class.

Note: When calling the variable do not prefix with the $ sign as you would in other situations.

Configure PHP on Windows Server 2003

These instructions were tested using PHP 5.2.9 on Windows Server 2003 sp2 with IIS 6 installed.

1. Download PHP from http://www.php.net, make sure you use the .zip package NOT the installer.

2. Extract .zip file, C:PHP is the recommended location

3. Add C:PHP to your path. Right click My Computer and select Properties. On the Properties dialog select the Advanced tab and click Environmental Variables. Under the System variables select the path entry and click edit. Find the last entry enter a semi colon (;) followed my C:PHP

4. Open the Internet Information Services (IIS) Manager from Administrative Tools.

a) Web Service Extension. Click down to the Web Service Extension folder. Right click the folder and select Add New Web Service Extension. Set Extension Name to .PHP and add C:PHPPHP5ISAPI.DLL to Required Files . Check Set Extension Status To Allowed.

b) Web Sites. Click down to Web Sites. Right click the folder and select Properties. From the Home Directory tab click the Configuration button. Click Add to add an Application Extension. Enter C:PHPPHP5ISAPI.DLL as Executable and PHP as Extension. Leave the rest as default and click Ok.

5. Finally set the correct file permissions the C:PHP folder. If left as default you will probably get a 401.3 error when trying to open php file.

6. You should now be able to execute php script from the C:INETPUBWWWROOT folder. To test this create an info.php file and add phpinfo() in a php scripting block.

Save, then navigate to http://localhost/info.php

Other things worth noting

– As you start using PHP, in the not too distant future you will probably need to make some changes to php.ini. PHP works fine without the php.ini file but you really should have one and it should be in your Windows directory. Copy C:PHPPHP.INI-RECOMMENDED to C:WINDOWSPHP.INI

– If you are going to use MySQL you will need to make sure to uncomment the line “extension=php_mysql.dll” in php.ini and copy C:PHPLIBMYSQL.DLL to C:WINDOWSSYSTEM32 (Simply setting the PATH won’t work as this is apparently hard coded in PHP5). If you don’t follow these steps you will get an error message similar to this: “Call to undefined function mysql_connect”