Using the .NET Assembly in PHP - Creating an Assembly
(Page 2 of 4 )
Open Visual Studio.Net and create a new class library project, which we will call "phpclass". Click on the OK button when you're done, as shown below:
In the code window for class1 (class1.vb), type the following:
Namespace HealthRecord
Public Class patient
Private m_lmp As Date
Public Property lmp()
Get
Return Format(m_lmp, "D")
End Get
Set(ByVal Value)
m_lmp = Value
End Set
End Property
Public ReadOnly Property edd()
Get
'EDD is 280 days from LMP
Return Format(DateAdd(DateInterval.Day, 280, m_lmp), "D")
End Get
End Property
End Class
End Namespace Let's assume that we're coding this app for a clinic. This VB.NET code will calculate the EDD (Estimated Delivery Date) from the specified LMP (Last Menstrual Period) date for any patient. To implement this, we have created a namespace called HealthRecord and a public class called patient, with two public properties: lmp and edd. Edd is a read-only property, which is calculated from lmp, which can be set and also read.
We have used the format function to format the date variable to a human readable format. We have also used the dateadd function to calculate the edd, which is 280 days from the lmp.
Creating a Strong Name Key File A strong name is the full class name including the namespace, version, public key and digital signature. All assemblies installed on the system have to be uniquely identified and versioned. To make sure the assembly is not tampered with, it needs to be signed with a common key (public key) to decrypt.
The strong name command-line tool (sn.exe) can be used to generate a new public-private key pair and to write that pair to a file which can then be used to create an assembly.
sn –k mykey.snk This utility can be found in the .NET Framework's Bin folder C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin (on my computer).
Alternatively, you could run a .NET Command Window by selecting Start –> Programs -> Microsoft Visual Studio .NET -> Visual Studio .NET Tools >- Visual Studio .NET Command Prompt, which sets all of the required paths for you.
Here is the sample output you should be expecting:
Copy this file (mykey.snk) to your project folder c:\tgol\phpclass (on my computer).
Adding the Strong Key to Project We need to add the key we just created to our project. Open the solution explorer and double click AssemblyInfo.vb to open the code window and add the following line:
<Assembly: AssemblyKeyFile("c:\tgol\phpclass\mykey.snk")> Make sure you enter the full path to where you saved the key file. Now build the application to create phpclass.dll (this file shall be created in the bin folder under the project folder) c:\tgol\phpclass\bin\phpclass.dll (on my computer).
Registering the Assembly To register a .NET class with COM, you must run a command-line tool called the Assembly Registration Tool (regasm.exe). Regasm.exe creates a type library (TBL File) and adds information about the class to the system registry, so that COM clients can use the .NET class transparently.
regasm c:\tgol\phpclass\bin\phpclass.dll /tlb:phpclass.tlb This utility can be found in C:\WINNT\Microsoft.NET\Framework\v1.0.3705 (on my computer).
Adding the Assembly to Global Assembly Cache In order to share the assembly with all applications, it needs to be installed in the GAC (Global Assembly Cache) with the following command from the command line:
gacutil /i c:\tgol\phpclass\bin\phpclass.dll This utility can be found in C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin (on my computer).
To view all of the assemblies installed in the GAC on your computer, open your windows explorer and look in the assembly folder under your windows folder, C:\WINNT\assembly (on my computer)
Next: Creating the PHP File >>
More PHP Articles
More By Jayesh Jain