Methods

DOTNET class constructor. assembly_name specifies
which assembly should be loaded, and class_name
specifices which class in that assembly to instantiate. You may
optionally specify a codepage to use for unicode
string transformations; see the COM class
for more details on code pages.

The returned object is an overloaded object, which means that PHP does
not see any fixed methods as it does with regular classes; instead, any
property or method accesses are passed through to COM and from there to
DOTNET. In other words, the .Net object is mapped through the COM
interoperability layer provided by the .Net runtime.

Once you have created a DOTNET object, PHP treats it identically to any
other COM object; all the same rules apply.

User Contributed Notes 10 notes

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:

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.

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.

For strongly-named NET assemblies that are registered in the GAC, you can just use the assembly namee.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 stringe.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).