com functions in php4 (windows)
alain m. samoun
introduction
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:
<?php
$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:
<?php
$instance->[object]->[method1]->[method2]->..->[property];
?>
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):
<?php
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:
<?php
$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:
for 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 hkey_classes_root 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 clsid subfolders.
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: http://msdn.microsoft.com/library/officedev/off2000/xltocobjectmodelapplication.htm
using php4 com functions with ms word
now, we have all we need to start with the first code example:
<?php
#*********************************************************
# this example, slightly modified from the zend site,
# will open an instance of word with a new
# document with the name "useless test.doc" and the line:
# "this is a test2..." typed inside.
#*********************************************************
#instantiate the word component.
$word = new com("word.application") or die("unable to instantiate word");
#get and print its version
print "loaded word, version {$word->version}<br>";
#another way to get the version using com_get
$testversion = com_get($word->application,version);
print "version using com_get(): $testversion <br>";
#make it visible in a window
$word->visible = 1;
#open a new document
$word->documents->add();
#write something
$word->selection->typetext("this is a test...");
#now save the document
$word->documents[1]->saveas("useless test.doc");
#comment next line if you want to see the word document,
#then close word manually
$word->quit();
#comment if you want to see the word document, then close
?>
if you study this example for a few minutes using the ole documentation that comes with word, you will learn practically all you need to write your own program.