The com class

(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)


The com class allows you to instantiate an OLE compatible COM object and call its methods and access its properties.


class com extends variant {
/* 方法 */
public __construct(
    string $module_name,
    array|string|null $server_name = null,
    int $codepage = CP_ACP,
    string $typelib = ""

Overloaded Methods

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.

com examples

示例 #1 com example (1)

// starting word
$word = new com("word.application") or die("Unable to instantiate Word");
"Loaded Word, version {$word->Version}\n";

//bring it to front
$word->Visible 1;

//open an empty document

//do some weird stuff
$word->Selection->TypeText("This is a test...");
$word->Documents[1]->SaveAs("Useless test.doc");

//closing word

//free the object
$word null;

示例 #2 com example (2)


= 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();
$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++) {
$fld[$i]->value "\t";
$rowcount++;            // increments rowcount


$rs null;
$conn null;