Creating COM Objects for PHP
Posted: (EET/GMT+2)
Creating COM Objects for PHP
Nov 19, 2000
PHP 4 & Borland Delphi 5
If you're a fan of PHP, you've probably noticed that the Windows version of PHP has support for Microsoft's Component Object Model, or COM for short. Although PHP has support for many awesome features, there still are times when they aren't enough. Read on to learn how to extend PHP with custom COM objects.
In PHP for Windows, you can easily access any COM object by using the COM() function to create a COM object. For instance, the following PHP script can be used to call a test COM object:
<HTML>
<H2>Testing COM objects from PHP 4</H2>
<P>The current IP address is:
<?php
$obj = new COM("PHP4COM.PHP4COMDemo");
$ip = $obj->fstrGetIPAddress();
echo $ip;
?>
</P>
</HTML>
Above, the code constructs a COM object and calls a function (method) on it. Actually, this COM object was created using Borland Delphi 5, which provides excellent support for COM. Next, you will learn how this COM object was created.
Creating COM objects with Delphi
If you want to create COM objects that can be called from PHP applications, you need to create a so-called Automation Library. To do so, first choose the New command from the File menu and then click the ActiveX tab:

Next, select the ActiveX Library icon, and click OK. Delphi will then create a simple skeleton, which you can extend with an automation library.
An ActiveX Library doesn't do anything by itself, so you must add an automation object to it. Activate the New Items dialog box again by choosing the menu command File | New, but this time choosing the Automation Object icon on the ActiveX tab:

By clicking OK, Delphi will display the Automation Object Wizard:

Here, you will need to enter a CoClass name for your COM object. For the purposes of this demo application, enter PHP4COMDemo.
Using the type library editor
Once you have entered a name for your CoClass, Delphi will open the type library editor window:

Although it seems difficult at first, there's no need to be afraid. Instead, click the small lime green arrow button to add a new method to the IPHP4COMDemo interface. Name this method fstrGetIPAddress, and as the parameters, enter the following:
| Name | Type | Modifier |
| ReturnValue | BSTR * | [out, retval] |
Note that the name of the parameter is not important. Also, you need to manually to type in the star (*) after the BSTR to indicate a pointer value. After this has been done, you are ready to type in the code for the method you have just defined.
Writing the method code
To start typing in the code for the method you've just defined, click the Refresh button on the type library editor, and then return to the code editor. There, you should already see the function definition for the method.
Type in the following code:
function TPHP4COMDemo.fstrGetIPAddress: WideString;
Var
iResult : Integer;
cHost : Array[0..255] of Char;
heInfo : PHostEnt;
begin
iResult := GetHostName(cHost,SizeOf(cHost));
If (iResult = 0) Then Begin
Result := cHost;
heInfo := GetHostByName(cHost);
If (heInfo <> nil) Then Begin
Result := Inet_NToA(In_Addr(PInAddr(heInfo.h_addr^)^));
End
Else Result := '127.0.0.1'; { localhost }
End
Else Result := '(unknown)';
end;
Of course, this code uses the WinSock API to query the IP address of the local computer. For this, the code uses the GetHostName and GetHostByName methods to retrieve the first IP address of the computer.
As you might know, the WinSock DLL must be initialized before it can be used. For this, the following code must be added to any unit in the project:
Var wsTemp : TWSAData; initialization WSAStartup($0101,wsTemp); finalization WSACleanup; end.
Testing the object
As noted previously, calling methods of a COM object is very simple from a PHP script. Download the sample code by using the link below, and the run the COMTest.php script through a PHP 4.0.2 interpreter (or later). Of course, you must register the COM object into the Registry before it will work. Use Delphi's Register ActiveX Server command on the Run menu to do this.
If everything goes smoothly, you should see your computer's IP address in the middle of the HTML code. Congratulations, you've just called your custom COM object from a PHP script!
Download the example code
Download creatingcomobjectsforphp.zip (175 kB) which contains the sample library PHP4COM. Please note that the sample application will require at least Borland Delphi 5 Professional or better. Also, to run the PHP script you will need PHP 4.0.2 or later. You can download the PHP interpreter from www.php.net.
* * *
Need help using PHP and/or Delphi? Let us know!