搞完了PHP4調用JavaBean,又想去試試調用COM,開始以為很難,自己用VB6寫了一個Active Dll在PHP4中調用,馬上成功,比調用javabean方便多了,下面講一下我的步驟。
一:用VB6寫Activex Dll
代碼如下:
Option Explicit
Private MyScriptingContext As ScriptingContext
Private MyApplication As Application
Private MyRequest As Request Private MyResponse As Response
Private MyServer As Server
Private MySession As Session Public
Sub OnStartPage(PassedScriptingContext As ScriptingContext)
Set MyScriptingContext = PassedScriptingContext
Set MyApplication = MyScriptingContext.Application
Set MyRequest = MyScriptingContext.Request
Set MyResponse = MyScriptingContext.Response
Set MyServer = MyScriptingContext.Server
Set MySession = MyScriptingContext.Session
End Sub
Public Sub OnEndPage()
Set MyScriptingContext = Nothing
Set MyApplication = Nothing
Set MyRequest = Nothing
Set MyResponse = Nothing
Set MyServer = Nothing
Set MySession = Nothing
End Sub
Public Function Test_Number(num) As Variant
If num < 0 Then Get_Number_Attrib = -1
If num > 0 Then Get_Number_Attrib = 1
If num = 0 Then Get_Number_Attrib = 0
End Function
具體方法如下:新建一個VB6工程,ActiveX Dll將工程命名為P_test,類名為c_test
類的文件內容如上。
編譯生成p_test.dll文件
二:注冊
提示符下運行:regsvr32 p_test.dll
三:編寫php文件,test.php4代碼如下:
<?
$b=new COM("p_test.c_test");
$a=$b->Test_Number(-454);
echo $a;
?>
運行php4文件將顯示-1
可能遇到的問題是,編譯工程時通不過,要將
Microsoft Active Server Pages Object Library
引用進來,具體實現“Project->References”找到改庫,並勾上
相比之下,PHP4調用com應該比PHP4調用javabean好哦,因為畢竟是Ms系統嘛。大家也可以去自己編寫調用數據庫的控件,用PHP4調用,從某種程度上,和PHP調用javabean一樣,可以說,實現了“隱藏源代碼”。
歡迎有興趣的朋友交流。