Swoole\Event->set

Declaration

<?php
Swoole\Event::set(int $fd, mixed $read_callback, mixed $write_callback, int $flags)\:\ bool

Parameters

fd

fd of the socket type

read_callback

Readable callback function

write_callback

Writeable callback function

flags

Flags: SWOOLE_EVENT_READ SWOOLE_EVENT_WRITE SWOOLE_EVENT_READ|SWOOLE_EVENT_WRITE

Return

success

If success, it returns TRUE, meaning the event listener was successfully added

false

Otherwise it returns FALSE, meaning there was an issue registering the event

Description

Update the read, write callback function and flags registered on the Event Loop, which is based on the file descriptor passed in. The Event::set method is basically the same as Event:add but allows you to update an existing registered event listener. If the file descriptor passed in does not exist, false is returned because the fd does not exist.

When either the read or write functions are set to null, nothing is modified, allowing you to only change the flags.

Only call Event::del when clearing an event listener, by doing this, the event loop will release read_callback or write_callback when a event callback is cleared, again, this must be done when wanting to perform other operations on a socket. You can use Event:set to alter the status and event monitoring for the file descriptor.

Note: If you enable the SWOOLE_EVENT_READ event, but it is not currently set up via the read_callback, false will be returned. The same is true for SWOOLE_EVENT_WRITE.

Example

<?php

$fd = inotify_init();

// Setup a new event listener for inotify read events
Swoole\Event::add($fd, function () use ($fd) {

    $var = inotify_read($fd);
    var_dump($var);

});

// Set to monitor and listen for read events for the given $fd
Swoole\Event::set($fd, null, null, SWOOLE_EVENT_READ);