PHP Conference Japan 2024

com_event_sink

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

com_event_sink將 COM 物件的事件連接到 PHP 物件

說明

com_event_sink(variant $variant, object $sink_object, array|string|null $sink_interface = null): bool

指示 COM 將 variant 產生的事件導入 PHP 物件 sink_object

使用此功能時請務必小心;如果您執行的操作類似以下範例,那麼在網頁伺服器環境中執行它並沒有什麼意義。

參數

variant

sink_object

sink_object 應該是一個類別的實例,其方法名稱與所需 dispinterface 的方法名稱相同;您可以使用 com_print_typeinfo() 來協助產生範本類別以達到此目的。

sink_interface

PHP 會嘗試使用與 variant 相關聯的類型程式庫所指定的預設 dispinterface 類型,但您可以透過將 sink_interface 設定為您要使用的 dispinterface 名稱來覆蓋此選擇。

回傳值

成功時回傳 true,失敗時回傳 false

更新日誌

版本 說明
8.0.0 sink_interface 現在可以為 null。

範例

範例 #1 COM 事件接收器範例

<?php
class IEEventSinker {
var
$terminated = false;

function
ProgressChange($progress, $progressmax) {
echo
"下載進度: $progress / $progressmax\n";
}

function
DocumentComplete(&$dom, $url) {
echo
"文件 $url 下載完成\n";
}

function
OnQuit() {
echo
"已結束!\n";
$this->terminated = true;
}
}
$ie = new COM("InternetExplorer.Application");
$sink = new IEEventSinker();
com_event_sink($ie, $sink, "DWebBrowserEvents2");
$ie->Visible = true;
$ie->Navigate("http://www.example.org");
while(!
$sink->terminated) {
com_message_pump(4000);
}
$ie = null;
?>

注意事項

警告

在 PHP 8.0.0 之前,從任何事件處理器中呼叫 exit() 函式是不支援的,並且可能導致 PHP 程式當掉。可以透過從事件處理器中拋出例外,在主程式碼中捕捉例外,然後從那裡呼叫 exit() 函式來解決這個問題。

另請參閱

新增註解

使用者貢獻的註解 2 則註解

veggie
7 年前
我讓語音辨識可以運作了。我不確定為什麼我呼叫 sink 函式的方式讓它可以運作,但我現在更注重結果。這個小範例讓我笑到在地上打滾。

<?php
/*
* Search this for more info on the voice stuff:
* Automation Interfaces and Objects (SAPI 5.4)
*/

//directions:
//php friend.php
//then fire up windows voice recognition and turn it on and say stuff

$voice = new COM("SAPI.SpVoice");

print
"Hit control+c to end.\n";
print
"Friend: Hello friend!\n";

$voice->Speak("Hello friend!");

class
listen
{

function
Recognition($StreamNumber, $StreamPosition, $RecognitionType, $ISpeechRecoResult)
{

$phrase = $ISpeechRecoResult->PhraseInfo;
$text = $phrase->GetText();

print
"\nYou:$text\n";


global
$voice;
$say = array('oh', 'nice', 'humm', 'interesting', 'you dont say', 'uh huh', 'right', 'what', 'ha ha', 'you have got to be joking', 'right back at you buddy');
$idx = rand(0, count($say)-1);

print
"Friend: " . $say[$idx] . "\n";
$voice->Speak($say[$idx]);

}

}

$recog = new COM("SAPI.SpSharedRecognizer");

$context = $recog->CreateRecoContext();

//SRERecognition = 16 (default)
//SREAllEvents = 393215
//$context->EventInterests = 393215;

//try to listen to events on context
$listen = new listen(); //event handler
if (!com_event_sink($context, $listen, "RecognizerStateChange"))
{
print
"Unable to sink events\n";
exit;
}

$grammar = $context->CreateGrammar();

$i = $grammar->DictationLoad();

$s = $grammar->DictationSetState(1); //1=on, 0=off

while(true)
{

if(!
com_message_pump(1000))
{
print
".";
}

}

?>
fjortiz
19 年前
如果有人需要 ADODB.Connection 事件的 skeleton sink

class ADOConnectionEventSink {

function BeginTransComplete( $translevel, $objerror, $status, $objconn ) {
return 0;
}

function CommitTransComplete( $objerror, $status, $objconn ) {
return 0;
}

function RolbackTransComplete( $objerror, $status, $objconn ) {
return 0;
}

function WillConnect ( $ConnectionString, $userid, $psword, $options, $status, $objconn ) {
return 0;
}

function ConnectComplete ( $objerror, $status, $objconn) {
return 0;
}

function Disconnect( $status, $objConn ) {
return 0;
}

function WillExecute ( $src, $cursortyp, $locktyp, $options, $status, $objcomm, $objrs, $objconn ) {
return 0;
}

function ExecuteComplete ( $recaffected, $objerror, $status, $objcomm, $objrs, $objconn ) {
return 0;
}

function InfoMessage ( $objerror, $status, $objconn) {
return 0;
}
}

// 之後...
$db = new COM("ADODB.Connection", NULL, $charPage);
$sink = new ADOConnectionEventSink();
com_event_sink($db, $sink, "ConnectionEvents");
//...
To Top