Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

PHP native return types are now enforced

PHP provides native interfaces: they include methods and their type. Until PHP 8.1, such types were not enforced, for backward compatibility reasons. Nowadays, these types are enforced, just like any other interface. It makes PHP native interfaces on the same footing as custom interfaces.

In case this migration is too ambitious, it is possible to use the #[\ReturnTypeWillChange] to suppress the warning.

PHP code

<?php

class x implements Iterator {

function __construct() {
	print __METHOD__;
}

public function current()  {}
public function key(): mixed {}
public function next(): void {}
public function rewind(): void {}
public function valid(): bool {}
} 

new x; 

?>

Before

x::__construct

After

PHP Deprecated:  Return type of x::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice 

Deprecated: Return type of x::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice 
x::__construct

PHP version change

This behavior changed in 8.1.

Error Messages

Extension

© 2023-2026, Damien Seguy - Exakat