Com_dotnet

Com_dotnet

Introduction

The new opportunities in the object-oriented method in PHP 5 are not limited to private, protected and public variables in your script which allow to integrate the support of the external object models such as COM, Java, .Net and Corba.

What's Gone

The following functions are not supported in the PHP 5COM.


com_addref(), com_release() - your script should not worry about refcounts


com_get(), com_set(), com_invoke(),com_propget(), com_propset(), com_propput() - use regular PHP object-oriented syntax for getting or setting properties or invoke methods.


com_isenum() and $com->Next() – now use syntax foreach()


com_load() – now use operator new and integrated COM class

So, all the nasty stuff has been removed. And now let’s look at the innovations that have been added.

Iterators

If you have ever worked with VBscript while writing the ASP sites or vbs admin scripts you must see the following code:

<% 
set domainObject = GetObject("WinNT://Domain") 
for each obj in domainObject 
  Response.write obj.Name & "<br>" 
next 
%>

In PHP 4 COM, the equivalent code looks like this:

<?php  
$domainObject 
= new COM("WinNT://Domain");  
while (
$obj $domainObject->Next()) {  
  echo 
$obj->Name "&lt;br&gt;";  
}  
?>

If you've worked with VB/COM, looking at that code you can understand what exactly the Next() thing is doing; it is not a real method, and you certainly can't do that same thing in other languages that support COM. In PHP 5, this syntax has been dropped in favour of the much more natural foreach() statement:

<?php 
$domainObject 
= new COM("WinNT://Domain"); 
foreach (
$domainObject as $obj) { 
  echo 
$obj->Name "<br>"

?>

Exceptions

In PHP 4 there were no opportunities for realizing the error handler triggered from within COM code when there appeared an error. PHP allowed to install E_WARNING so that you would know that there was an error, but you couldn’t know precisely where the error occurred, nor be able to handle it programmatically.

In PHP 5 there is new construction that allows exceptions handling (try, catch() ? throw()) using integrated class com_exception. If you want to catch errors in your scripts, you have to write the following code:

<?php  
$com 
= new COM("...");  
try {  
  
$com->call_a_method();  
}  
catch (
com_exception $e) {  
  print 
$e "\n";  
}  
?>

Inside the catch block you can handle the error as is appropriate to your script. The com_exception class extends the default exception class provided by PHP, and so it has all of its methods. The COM exception/error code is made available via the getCode() method of the class.

Variant type

One of the not very well documented features of COM in PHP 4 is its VARIANT type support. If you haven’t worked with that data type you can present them as a COM equivalent of the PHP variable that can include different types of the data.

In PHP 5, the variant support has been greatly simplified by adopting the premise that we should only convert a variant value to a PHP type when there is a direct 1:1 mapping. In other cases we represent the variant type as an overloaded object. As a result we have cleaner code. If you work with variant arrays you don’t need to copy the content of the initial array to the PHP array. New object-oriented model in PHP 5 allows getting the access to the variant object as though it were an array.

Parameter transferring by reference (ByRef)

When you are working with COM objects you can use such method as parameter transferring by reference. In PHP 4 the only way of fulfilling that method is to create an instance of a VARIANT and set its ByRef manually. Object-oriented model in PHP 5 allows engine to get information about method from the COM object. It allows you to call these methods without thinking about the method of the variables transferring - the values are set for you automatically.

Event handling

Actually it is not the innovation in PHP 5 (it has been added in PHP 4) but we have to mention it here. Quite often you need to bind some event to a COM object. In VB there is used such operator as WithEvents if variable is defined, and Visual Basic will call the method automatically. In PHP there are some differences.

If you want to handle events from a COM object you need to create another object that will get these events. To do this, you create an instance of the class, and then bind the events to it:

<?php 
class IESink 
    public 
$terminated false
    public function 
OnQuit() { 
        
$this->terminated true
    } 


$ie = new COM("InternetExplorer.Application"); 
$ie->Visible true
$ie->Navigate("http://www.php.net/"); 

$sink = new IESink
com_event_sink($ie$sink"DWebBrowserEvents2"); 
while (!
$sink->terminated) { 
    
com_message_pump(4000); 


print 
"finished!\n"
?>

That script launches Internet Explorer and browses to the PHP home page (http://www.php.net/) and will be waiting when you quit the browser before continuing. You should note that the com_event_sink() function is responsible for sinking events from $ie to $sink and that it uses an interface named "DWebBrowserEvents2"

.Net support

In PHP 5 there is integrated the .Net support. To be more specific, it supports the instantiation of objects defined in .Net assemblies via the COM interoperability layer for .Net. So, we can say that PHP “sees” the .Net objects as though they were COM objects.

<?php  
  $stack 
= new DOTNET("mscorlib""System.Collections.Stack");  

  
$stack->Push(".Net");  
  
$stack->Push("Hello ");  

  echo 
$stack->Pop() . $stack->Pop();  
?>

Thus PHP 5 provides with the convenient access to the .Net class library. If you want to use all features you have to install .Net RunTime on your server.


 

  • Top