Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 25.x
Latest version:
pecl install openswoole-25.2.0
<?php Swoole\Event::set(int $fd, mixed $read_callback, mixed $write_callback, int $flags)\:\ bool
fd of the socket type
Readable callback function
Writeable callback function
Flags:
SWOOLE_EVENT_READ
SWOOLE_EVENT_WRITE
SWOOLE_EVENT_READ|SWOOLE_EVENT_WRITE
If success, it returns TRUE, meaning the event listener was successfully added
Otherwise it returns FALSE, meaning there was an issue registering the event
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_READevent, but it is not currently set up via theread_callback,falsewill be returned. The same is true forSWOOLE_EVENT_WRITE.
<?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);