PHPIDS on php 8.1 Deprecated Errors

PHPIDS on php 8.1 Deprecated Errors

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

PHPIDS on PHP 8.1 Deprecated Errors

Upgrading from PHP 7.x to PHP 8.1 often brings a flood of deprecation notices, particularly when dealing with legacy libraries that were written for older language versions. This is exactly what happens when you run the PHPIDS (PHP Intrusion Detection System) on a modern server. If you are seeing errors regarding IDS\Report::count() and IDS\Report::getIterator(), it is not a logic error in your application, but rather a compatibility issue between how PHP 8.1 handles interfaces and how the library implements them.

The specific error messages indicate that the return types of these methods are incompatible with their respective interface contracts:

PHP Deprecated: Return type of IDS\Report::count() should either be compatible with Countable::count(): int...
PHP Deprecated: Return type of IDS\Report::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable...

In PHP 8.1, the language enforces strict return type compatibility for classes implementing specific interfaces like Countable and IteratorAggregate. Previously, you could implement these interfaces without declaring a return type on the method, assuming the implementation returned an integer or an iterator respectively. However, PHP 8.1 requires explicit type declarations to match the interface definition (int for count() and \Traversable for getIterator()).

The source code provided shows the problematic implementation in Report.php:

public function count()
{
    return @count($this->events);
}

public function getIterator()
{
    return new \ArrayIterator($this->events);
}

While these methods technically return the correct data types, they lack the explicit : int and : \Traversable type hints required by the new language standard. The fact that the code uses the @ error suppression operator in the count() method is a common pattern in legacy codebases to hide notices about non-existent properties, but it does not resolve the signature mismatch here.

To fix this permanently and ensure everything works perfectly without relying on temporary suppression attributes, you need to update the class definitions. This is a straightforward change that aligns the implementation with modern PHP standards.

Open your IDS/Report.php file and modify the method signatures as follows:

public function count(): int
{
    return count($this->events);
}

public function getIterator(): \Traversable
{
    return new \ArrayIterator($this->events);
}

There are two key improvements here. First, we remove the @ error suppression operator from the count() method. Since the method now has a proper return type hint matching the interface, PHP will no longer complain about the potential issue with non-existent properties if the $events array is handled correctly elsewhere. Second, we explicitly declare the return types. Note that for getIterator(), \ArrayIterator implements \IteratorAggregate, so returning it satisfies the \Traversable contract defined in the interface.

This solution removes the need to use the #[\ReturnTypeWillChange] attribute, which is intended only as a temporary workaround. By fixing the signatures directly, you ensure

Our network