Hudzilla.org - the homepage of Paul Hudson
Contents > Java and COM > Getting started with COM Wish List | Report Bug | About Me ]

14.1.2     Basic COM

This is NOT the latest copy of this book; click here for the latest version.

To get started with COM, the first script to create simply fires up a COM component, and calls a function on it. The COM component we will use for this is WScript.Shell, which is a very simple COM component to get started with. "WScript", by the way, is the Windows Scripting Host; Microsoft's script management software. The various components that WSH offers us give almost unprecedented flexibility.

Here is our first script, just four lines long:

<?php
    $shell
= new COM("WScript.Shell");
    
$shell->Run("notepad.exe");
    
$shell = null;
?>

Author's Note: In PHP 4, you needed to explicitly release all references to your COM objects. This is no longer necessary in PHP. Strictly speaking, setting the objects to NULL is also not necessary, but, hey, I'm a clean freak.

You should be able to see what that script does even without running it, because the code is nice and clear. If you hadn't guessed, it simply runs Notepad on the computer where the script was executed - nothing special, given that we can just exec() in PHP to execute outside functions, but it should at least get you started thinking in terms of COM.

Note that in line one we create the COM object, $shell, by passing "Wscript.Shell", the name of the component we want to create, to the constructor of the COM class. This returns $shell, a working WScript.Shell object, on success, otherwise PHP will issue an error and return false.

In line two, the run() function of WScript.Shell is used, which runs an executable file. It is not really within the scope of this book to discuss the workings of WScript.Shell or other COM objects, but try searching Google exactly like this:

"wshshell object" msdn

Make sure you keep the quotes intact - all being well, the Microsoft Developer Network (MSDN) should be the first link, giving the full documentation for this object, as well as other related components.

Lines three and four free the COM component created on line one, then clears the variable. This might seem odd given that PHP frees its resources when it closes, however the nature of COM means that each object has a reference count that needs to be decremented when the object is no longer used - PHP does not do this, so the object sits around, unused on your server, for several minutes after your script finishes.

As a result, you should always set your objects to null - this will ensure full garbage collection. As mentioned elsewhere, it is generally smart to always do your housekeeping yourself - relying on PHP to clean up your after your script is sloppy and haphazard.





<< 14.1.1 Instantiating an object   14.1.3 Advanced COM >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is nine plus eight?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow