Skip to content

MySqliDriver::getResource throws in PHP 8 instead of returning null whe the resource has been closed prior to the call #409

@dakujem

Description

@dakujem

Version: 4.2.5
PHP: 8.0, 8.1 (does not happen on PHP 7)

Bug Description

MySqliDriver::getResource() throws an error in PHP 8 and is thus not suppressed by the @ operator.

Fatal error: Uncaught Error: mysqli object is already closed

The problem is when this happens during shutdown with EALL | E_STRICT setting, when the connection service is being destructed.
I'm sharing the resource and it happens to be closed before the driver is destructed.

Steps To Reproduce

$driver = new MySqliDriver( ... );
$driver->getResource()->close();
$driver->getResource();

Expected Behavior

I expect null to be returned instead of an internal error being thrown.

Possible Solution

    public function getResource(): ?\mysqli
    {
        if (!is_resource($this->connection)) {
            return null;
        }
        return @$this->connection->thread_id ? $this->connection : null;
    }

or

   public function getResource(): ?\mysqli
   {
       try {
           return @$this->connection->thread_id ? $this->connection : null;
       } catch (\Error $e) {
           return null;
       }
   }

Let me know which alternative is better and I'll prepare a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions