The built-in COM functionality of PHP4 is quite attractive for some of us programming in the win32 environment.
So far, there is not much documentation on the subject. This short article will explain how to use COM in real
PHP4 programming with three examples using MS office 2000 Word and Excel programs and the Adobe Distiller program.
The COM technology has been developed by Microsoft for several years, under different names. As far as this article is concerned,
the words OLE, OLE Automation, ActiveX and COM are all the same: They designate an encapsulated piece of code
(the Object) that performs some functions
for a windows application. PHP4 COM connects to the object (Instantiate the object) and uses its methods and properties.
If you want to reproduce the following examples, here is my configuration:
- Windows 98 - MS Office 2000
- Apache 1.3.9 Windows
- PHP4.02 Dev (08-20-00) Running as CGI
COM tags in PHP4
Lets start with the specific information to use the COM functions with PHP4. To instantiate a component, one
needs the "new" operator
and the "OLE programmatic identifiers" of the object:
$instance = new COM('$identifier');
Since COM is a reserved class name in PHP4, it passes the object's
identifier to the constructor.
Now that we have instantiated the component, we can easily
reach its methods and properties, using the OOP class. For example:
It's that simple!
There are two tag functions for PHP4 COM that are used when the OOP construct doesn't work.
(In the case of PHP syntax problems, with the names and values of
properties with invalid characters, like dot or parenthesis):
bool com_set(class com_object, string property name, string property_value);
mixed com_get(class com_object, string property_name);
Finally, PHP4 also supports DCOM to create an instance of an object on a remote computer:
$Instance = new COM(string 'Component name', string 'remote_server_address');
Note: there is a DCOM directive to set in the PHP configuration. PHP developers may add DCOM support for Unix in the future.
That's all, there are no other functions to remember!
Identifiers, methods and properties.
Identifiers are strings like:
- MS Word: "Word.Application" or "Word.Application.9"
- MS Excel: "Excel.Application" or "Excel.Sheet"
- ADOBE Acrobat: "Exch.application" or "PdfDistiller.PdfDistiller"
As the last identifier name indicates, it is not always easy to know the right
name for the object. If you do not have access to a VBA doc, you can look at
the windows registry (Start - Run regedit) and look in the
folder: Scan down until the end of the extensions list, you will then reach the
Application names. The COM Identifiers available in your machine,
are the folders with the
The application program should document its COM methods and properties.
In the case of Office 2000, start the application, then open the visual basic editor with the <ALT+F11> short cut
key and select the Objects Editor <F2>. Enter a name of method or properties for the
application's library. Then, select a class or member and right click on the member name
in the next window below. You will get the description for the class or
member by selecting help.
You can also consult MSDN. An example for Excel is: