The com class

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

Introduction

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

Class synopsis

class com extends variant {
/* Methods */
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

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;

?>

Table of Contents

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial