The usual example shown for trapping for failure of COM instantiation
(Windows systems) is something like (see for example
http://at2.php.net/manual/en/class.com.php):
$word = new COM("word.appli cation") or
die("Unable to instantiate Word");
This is all well and good, but if you are using a command
line interface (CLI) version of PHP 5 then you might wish to
have a little more sophisticated error handling. Specifically,
I have a function that looks like so:
function getWinDir() {
$result = "";
$oScript = new COM("MSScriptCo ntrol.ScriptCon trol");
...
return $result;
}
How do I trap for a COM instantion error here? In particular,
if it errors, I want to return ":Error" so the calling function
can deal with it. I do not want the program to abort. Nothing
I've tried seems to work. For example, suppose I'm having a bad
spelling day and leave off the final l to ensure an error.
If I insert an @ in the line, it will suppress the error reporting
and the program will also halt. And PHP doesn't like me trying
$oScript = new COM("MSScriptCo ntrol.ScriptCon tro") or
($result = ":Error");
I've also tried using set_error_handl er and changing the
error level reporting with error_reporting and varying
$oldIni = ini_set("com.au toregister_verb ose", 0); // $oldIni is 1
none of which has produced any joy.
Whenever the @ is there, I get a silent failure and a full stop.
If it's missing it also stops with the following message:
Fatal error: Uncaught exception 'com_exception' with message
'Failed to create COM object `MSScriptContro l.ScriptContro' :
Invalid syntax' in C:\Testing\DirF ind.php:18
Thanks for any tips,
Csaba Gabor from Vienna