Skip to content

Fix isOpen() compatibility with PHP 8.0+ Socket object#17

Open
rajesh-varatharajan wants to merge 1 commit intoalexandr-mironov:masterfrom
rajesh-varatharajan:fix/php81-socket-isinstance
Open

Fix isOpen() compatibility with PHP 8.0+ Socket object#17
rajesh-varatharajan wants to merge 1 commit intoalexandr-mironov:masterfrom
rajesh-varatharajan:fix/php81-socket-isinstance

Conversation

@rajesh-varatharajan
Copy link
Copy Markdown

Problem

In PHP 8.0+, socket_create() returns a \Socket object instead of a
resource. The isOpen() check is_resource($this->socket) always
returns false on PHP 8.0+, causing the connection to appear closed
even when it is open.

Fix

Replace:

if (!is_resource($this->socket)) {
With:
if (!is_resource($this->socket) && !is_object($this->socket)) {

Using is_object() instead of instanceof \Socket ensures backward
compatibility with PHP 5.4+ (the minimum required version of this
package), since the \Socket class does not exist before PHP 8.0.

- PHP < 8.0: socket is a resource → is_resource() returns true
- PHP 8.0+: socket is a \Socket object → is_object() returns true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant