Skip to content
Advertisement

Getting id of PHP 8 Socket

With the new socket instance returning from socket_create() in php 8, how do we get a numeric or unique reference to this socket in the way casting to int would work in earlier versions. This is used for many things, including connection tracking in log files and storing metadata about connections when they are passed into/return from socket_select().

Before php 8:

$socket_resource = socket_create(...);
$socket_id = (int)$socket_resource;

After php 8

$socket_instance = socket_create(...);
$socket_id = (int)$socket_instance; // PHP Warning:  Object of class Socket could not be converted to int

There is a new function in php 8 get_resource_id($resource) which looks the same as casting to int, but this does not work on sockets.

Advertisement

Answer

You can use spl_object_id() with any object. It’s an arbitrary value that can be reused, but so are resource IDs.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement