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

14.1.4     The possibilities of COM

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

The two COM scripts presented so far should have given you a fair idea of the power of COM - we have managed to run programs, load and control web browsers, and grab user and PC information, all by re-using existing code.

However, in order to demonstrate quite how much flexibility COM gives you as a Windows-based PHP developer, we are going to do something as yet not done in the world of PHP: we are going to make PHP work with VBScript. If you just gasped and had to take a deep breath before continuing, you clearly did not migrate to PHP from ASP! Microsoft's Active Server Pages (ASP) technology lets you choose the language you write your scripts in on a script-by-script basis. Now, while I dislike ASP, and dislike VBScript even more, quite a few of you probably came from an ASP background.

ASP programmers traditionally make very heavy use of COM objects, from ADO to their own custom components; giving people the functionality to literally write and execute VBScript inside their PHP code adds a new layer of functionality. Granted, many will see absolutely no reason why VBScript and PHP should be mixed, but the hacker's answer to that is "because we can"!

Take a look at this script:

<?php
    $scripter
= new COM("MSScriptControl.ScriptControl");
    
$scripter->Language = "vbscript";

    
$i = 5;
    
$j = 2;
    
$k = $scripter->eval( $i . " + " . $j);
    print
"Result: $k\n";

    
$scripter->ExecuteStatement("MsgBox \"The result is $k\"");
    
$scripter = null;
?>

This time we use the component MSScriptControl.ScriptControl, the Microsoft Script Control, which is a control designed to let application designers execute scripts from within their programs. In this example, we want to execute VBScript from our PHP "application", so this control is perfect for our needs.

Line two of the PHP script tells the MS script control that we want to use VBScript. This could also have been JScript, Microsoft's extension of JavaScript, or it also could have been any other supported scripting system that was installed. We then use the eval() function to pass in the code we want to execute - in this case, it is just simply adding two variables together, and the result is stored in the PHP variable $k.

Next, the ExecuteStatement() function is called, which also takes the block of code to execute as its parameter. In this case, the MsgBox VBScript function is called, which pops up a message box on the screen with the parameter supplied to it, "The result is $k". Naturally PHP will replace the $k with the value received back from the earlier call to eval(), and MsgBox will output "The result is 7". Just so you can see that VBScript and PHP do work together fluently, the value is also printed out to the command line where you launched PHP.

Finally, the $scripter variable is set to null, to ensure that things are cleaned up properly.

So now we have a script where we call another scripting language - are you confused yet? Hopefully this should have given you ideas for your own scripts - do not be afraid to experiment with wild ideas!





<< 14.1.3 Advanced COM   14.1.5 DCOM >>
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 four plus six?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow