Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting id of PHP 8 Socket

Tags:

php

sockets

php-8

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.

like image 384
Drewster Avatar asked Oct 24 '25 17:10

Drewster


1 Answers

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

like image 76
Álvaro González Avatar answered Oct 27 '25 07:10

Álvaro González



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!