Skip to content

[PHP 8.4] Fatal Error: Declaration of Client::doRequest() must be compatible with AbstractBrowser::doRequest() #688

@remoover

Description

@remoover

Description

When running PHP 8.4 with the latest Symfony components (v7.x or v8.x, e.g., in Laravel 12), symfony/panther (v2.3.0 and dev-main) throws Fatal Errors immediately upon usage.

This is due to strict type enforcement in PHP 8.4 combined with the updated method signatures in the upstream Symfony\Component\BrowserKit\AbstractBrowser and Symfony\Component\DomCrawler\Crawler.
Steps to Reproduce

Install symfony/panther (v2.3.0)

Run on PHP 8.4 environment.

Ensure symfony/browser-kit and symfony/dom-crawler are at version ^7.0 or ^8.0.

Attempt to initialize the Client.

Error 1: Client.php

Exception:
code Text

Symfony\Component\ErrorHandler\Error\FatalError: Declaration of Symfony\Component\Panther\Client::doRequest($request) must be compatible with Symfony\Component\BrowserKit\AbstractBrowser::doRequest(object $request): object

Location: src/Client.php:294
Error 2: Crawler.php (occurs after fixing Error 1)

Exception:
code Text

Symfony\Component\ErrorHandler\Error\FatalError: Declaration of Symfony\Component\Panther\DomCrawler\Crawler::closest(string $selector): ?Symfony\Component\Panther\DomCrawler\Crawler must be compatible with Symfony\Component\DomCrawler\Crawler::closest(string $selector): ?static

Location: src/DomCrawler/Crawler.php:145
Proposed Fixes

To fix compatibility with the latest Symfony contracts and PHP 8.4, the method signatures need to be updated.

  1. Fix src/Client.php
    The doRequest method needs the return type hint : object.
    code Diff
  • protected function doRequest($request)
  • protected function doRequest(object $request): object
    {
    // ...
    }
  1. Fix src/DomCrawler/Crawler.php
    The closest method needs to return ?static instead of the specific class name to satisfy the parent class signature.
    code Diff
  • public function closest(string $selector): ?Crawler
  • public function closest(string $selector): ?static
    {
    // ...
    }

Environment

PHP: 8.4.12

Panther: v2.3.0 / dev-main

Symfony Components: ^7.0 / ^8.0 (Laravel 12 context)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions