PHP 5.6.18 is available

La clase DOTNET

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


La clase DOTNET le permite instanciar una clase desde un ensamble .Net y poder llamar sus métodos y acceder a sus propiedades.

$obj = new DOTNET("assembly", "classname")


DOTNET::DOTNET ( string $assembly_name , string $class_name [, int $codepage ] )

Constructor de la clase DOTNET. El parámetro assembly_name especifica cual ensamble debería ser cargado, y class_name especifica cual clase instanciar en ese ensamble. Opcionalmente puede especificar un parámetro codepage para usar transformaciones de cadena unicode; vea la clase COM para más detalles en las páginas de código.

El objeto devuelto es un objeto sobrecargado, lo que significa que PHP no ve ningún método fijo como lo hace con las clases regulares; en su lugar, cualquier método o propiedad que accede se pasa por medio del COM y de allí a DOTNET. En otras palabras, el objeto .Net es mapeado a través de la capa de interoperabilidad COM provista por el runtime de .Net.

Una vez se ha creado un objeto DOTNET, PHP lo trata idénticamente como cualquier otro objeto COM; se aplican todas las mismas reglas.

Ejemplo #1 Ejemplo DOTNET

= new DOTNET("mscorlib""System.Collections.Stack");
$stack->Push("Hello ");
$stack->Pop() . $stack->Pop();


Necesita instalar el runtime de .Net en su servidor web para tomar las ventajas de esta característica.

add a note add a note

User Contributed Notes 10 notes

juan156_elias at gmail dot com
8 months ago
Using COM and DOTNET directly is quite a nightmare. DOTNET only allows you to target .Net 3.5 and below, and all the binaries need to be COM  Visible. This basically means that you will need to write your own .Net binaries for everything, at least wrappers.

There is a library out there (NetPhp) built on top of the COM class that will solve all these issues, so you can get code like this:

$manager = new \NetPhp\Core\NetManager();
$manager->RegisterAssembly('mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089', 'mscorlib');
$manager->RegisterClass('mscorlib', 'System.IO.File', 'File');
$manager->RegisterClass('mscorlib', 'System.IO.FileOptions', 'FileOptions');
$file = $manager->Create('mscorlib', 'File');
$fileoptions = $manager->Create('mscorlib', 'System.IO.FileOptions')->Enum('Encrypted');

$file->Create("C:\\www\\prueba.tres", 2048, $fileoptions);
ricardopsj at gmail dot com
1 year ago
Create an Excel Workbook using DOTNET.


= 'Microsoft.Office.Interop.Excel, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c';
$full_class_name = 'Microsoft.Office.Interop.Excel.ApplicationClass';

$e = new DOTNET($full_assembly_string, $full_class_name);
$wb = $e->workbooks->add();
$Precios = $wb->Worksheets(1);
$Precios->Name = 'Precios';
$Venta = $wb->Worksheets(2);
$Venta->Name = 'Venta';
$Tons = $wb->Worksheets(3);
$Tons->Name = 'Tons';

$Meses = Array('2014-01', '2014-02', '2014-03', '2014-04', '2014-05', '2014-06', '2014-07', '2014-08', '2014-09', '2014-10', '2014-11', '2014-12');
foreach (
$Meses as $Numero => $Mes) {
$Precios->Range("A" . ($Numero+1))->Value = $Mes;



Go to c:\windows\assembly to know what value to put in $full_assembly_string.

If you don't know the assembly, use to browse it, use what you learn there to fill $full_class_name.


kschroeder at mirageworks dot com
6 years ago
In order to find out what the PublicKeyToken value is, open up Explorer and go to c:\windows\assembly.  In that directory you will see all of the registered assemblies along with their PublicKeyToken value.
m dot atifmajeed at gmail dot com
11 months ago
How to use donet class for creating object of parameterized constructor

private SGF ingerPrintManager m_FPM; //member variable
SGFPMDeviceName device_name = SGFPMDeviceName.DEV_ FDU02 ;
m_FPM = new SGF ingerPrintManager (device_name);
sandips (yardi)
4 years ago
exercise to try different type of method signature and calling them in php. Code
Public Class Class1
    Public Function SayHello(ByVal input As String) As String
        Return "Php input: " & input & "</BR>Dot net library said- 'Hello'"
    End Function
    Public Function ConcatArray(ByVal Values As Object()) As String
        Dim ret As String = String.Empty
        For Each sval As Object In Values
            ret = ret & sval.ToString & " "
        Return ret.Substring(1, ret.Length - 1)
    End Function
    Public Function ReturnObject(ByVal FirstName As String, ByVal LastName As String) As Name
        Dim obj As New Name
        obj.FirstName = FirstName
        obj.LastName = LastName
        Return obj
    End Function
'To explain Byref doesn't work with php
    Public Function CallByRef(ByRef value As String) As String
        value = "Value to ByRef parameter set in dot net."
        Return "ByRef CallByRef() method called."
    End Function
End Class

Public Class Name
    Public FirstName As String = String.Empty
    Public LastName As String = String.Empty
End Class

Php code to test .net library
= new DOTNET("DotNetTest,"
$class1->SayHello("Hi PHP!!!!");

$parameter[0] = 12345;
$parameter[1] = "MyString";
$parameter[2] = false;
$ret = $class1 -> ConcatArray($parameter);

$Obj = $class1 -> ReturnObject("Sandip","Shimpi");
$Obj->FirstName." ".$Obj->LastName;

$myByRef = "";
"Value of ByRef Variable before calling .net CallByRef() method: ".$myByRef;
$class1 -> CallByRef($myByRef);
"Now value of ByRef Variable: ".$myByRef;
sam at bitopia dot co dot uk
7 years ago
If you want to load any other assembly apart from mscorlib, you'll need to include it like the following

$x = new DOTNET('The.Assembly.Name, Version=X.X.X.X, Culture=neutral, PublicKeyToken=ZZZZZZZZZZZZ', 'The.Class.Name');

Replace all relevant details.
jan dot zahradnik at data3s dot com
2 years ago
PHP searches only GAC for .NET 2.0 runtime, so your DLL has to be targeted 2.0 - 3.5 framework.

Also it recognize only classes, not stucts, so you can't instantiate DateTime for example.
murat at muratyaman dot co dot uk
5 years ago
First, I hit the same mine:

"... Failed to instantiate .Net object [Unwrapped, QI for IDispatch] [0x80004002] No such interface supported ..."

In order to load the assembly and class in it successfully, I had to change my AssemblyInfo.cs file regarding the visibility of the types in it.

// some code above

// This sets the default COM visibility of types in the assembly to invisible.
// If you need to expose a type to COM, use [ComVisible(true)] on that type.
[assembly: ComVisible(true)]

// some code below
m holmes at windowlogic dot com dot au
6 years ago
For strongly-named NET assemblies that are registered in the GAC, you can just use the assembly name
e.g: $x = new DOTNET ("myAssembly", "myClass");

For strongly-named NET assemblies that aren't registered in the GAC, you need to use the full assembly string
e.g. $x = new DOTNET('myAssembly, Version=X.X.X.X, Culture=neutral, PublicKeyToken=ZZZZZZZZZZZZ', 'myClass');

You can't instantiate assemblies that haven't been strongly named.

"Strongly named" means that the assembly has a public key. To strongly name your own classes in Visual Studio, go to the Signing tab in the project properties and click the 'sign the assembly' box and choose a key file name.

To register an assembly in the GAC, there are various tools around to do that, but the easiest method is to drag-and-drop the compiled assembly into c:\windows\assembly using windows explorer (a shell extension is installed by default that handles registering dragged files).
5 years ago
As suggested before, you'll likely have to use a full assembly string including the assembly name, the assembly version (not the dll file version), the culture, and the public key token.  Otherwise, you'll get a 'file not found' catchable error.  To quickly find this on your own system to get the ball rolling, open C:\Windows\assembly in explorer (not in cmd as that shows a different view), scroll down the list until you find the assembly you want to work with, hover the mouse over it, and the tooltip shows the exact string to use (though processcorArchitecture if shown is optional).  You'll have to use .net utilities or other methods to automate collecting this info on other machines.

You'll also have to use the fully-qualified class name.  In other words, if you want to use the 'Form' class within 'System.Windows.Forms', you have to use the full class hierarchy 'System.Windows.Forms.Form'.  Otherwise you'll get an unhelpful 'Failed to instantiate .Net object' catchable error.

The following example shows the correct syntax to use the 'Form' class to pop up a blank form from php-cli, which can be used in leu of many Windows GUI PHP extensions out there.  Replace Version with your local version, and PublicKeyToken with your own local key token.


= 'System.Windows.Forms, Version=, Culture=neutral, PublicKeyToken=a8425bc35256e463';
$full_class_name = 'System.Windows.Forms.Form';
$form = new DOTNET($full_assembly_string, $full_class_name);

// code to add buttons, menus, text, etc


$form_event = '';
$form_event !== 'close') {

// handle form functions and events

To Top