(PHP 4 >= 4.1.0, PHP 5, PHP 7)
The COM class allows you to instantiate an OLE compatible COM object and call its methods and access its properties.
$obj = new COM("Application.ID")
$module_name
[, mixed $server_name
[, int $codepage
[, string $typelib
]]] )COM class constructor. The parameters have the following meanings:
NULL
, the object is run using the default for the
application. The default is typically to run it on the local machine,
although the administrator might have configured the application to
launch on a different machine.
If you specify a non-NULL
value for server, PHP will refuse to load
the object unless the configuration option
is set to TRUE
.
If server_name
is an array, it should contain the
following elements (case sensitive!). Note that they are all optional
(although you need to specify both Username and Password together); if
you omit the Server setting, the default server will be used (as
mentioned above), and the instantiation of the object will not be
affected by the
directive.
server_name key |
type | description |
---|---|---|
Server | string | The name of the server. |
Username | string | The username to connect as. |
Password | string | The password for Username . |
Flags | integer | One or more of the following constants, logically OR'd together:
CLSCTX_INPROC_SERVER ,
CLSCTX_INPROC_HANDLER ,
CLSCTX_LOCAL_SERVER ,
CLSCTX_REMOTE_SERVER ,
CLSCTX_SERVER and
CLSCTX_ALL . The default value if not
specified here is CLSCTX_SERVER if you also
omit Server , or
CLSCTX_REMOTE_SERVER if you do specify a
server. You should consult the Microsoft documentation for
CoCreateInstance for more information on the meaning of these
constants; you will typically never have to use them.
|
CP_ACP
(use system default ANSI code page - the
default if this parameter is omitted),
CP_MACCP
,
CP_OEMCP
, CP_SYMBOL
,
CP_THREAD_ACP
(use codepage/locale set for the
current executing thread), CP_UTF7
and CP_UTF8
. You may also use the number for a
given codepage; consult the Microsoft documentation for more details on
codepages and their numeric values.
The returned object is an overloaded object, which means that PHP does not see any fixed methods as it does with regular classes; instead, any property or method accesses are passed through to COM.
PHP will automatically detect methods that accept parameters by reference, and will automatically convert regular PHP variables to a form that can be passed by reference. This means that you can call the method very naturally; you needn't go to any extra effort in your code.
Example #1 COM example (1)
<?php
// starting word
$word = new COM("word.application") or die("Unable to instantiate Word");
echo "Loaded Word, version {$word->Version}\n";
//bring it to front
$word->Visible = 1;
//open an empty document
$word->Documents->Add();
//do some weird stuff
$word->Selection->TypeText("This is a test...");
$word->Documents[1]->SaveAs("Useless test.doc");
//closing word
$word->Quit();
//free the object
$word = null;
?>
Example #2 COM example (2)
<?php
$conn = new COM("ADODB.Connection") or die("Cannot start ADO");
$conn->Open("Provider=SQLOLEDB; Data Source=localhost;
Initial Catalog=database; User ID=user; Password=password");
$rs = $conn->Execute("SELECT * FROM sometable"); // Recordset
$num_columns = $rs->Fields->Count();
echo $num_columns . "\n";
for ($i=0; $i < $num_columns; $i++) {
$fld[$i] = $rs->Fields($i);
}
$rowcount = 0;
while (!$rs->EOF) {
for ($i=0; $i < $num_columns; $i++) {
echo $fld[$i]->value . "\t";
}
echo "\n";
$rowcount++; // increments rowcount
$rs->MoveNext();
}
$rs->Close();
$conn->Close();
$rs = null;
$conn = null;
?>