Req #77518 [Opn]: SeekableIterator::seek() should accept 'int' typehint as documented

From: Date: Thu, 24 Jan 2019 20:07:09 +0000
Subject: Req #77518 [Opn]: SeekableIterator::seek() should accept 'int' typehint as documented
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-219184@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=77518&edit=1 ID: 77518 User updated by: claude dot pache at gmail dot com Reported by: claude dot pache at gmail dot com Summary: SeekableIterator::seek() should accept 'int' typehint as documented Status: Open Type: Feature/Change Request Package: SPL related PHP Version: 7.3.1 Block user comment: N Private report: N New Comment: > unless I am mistaken, the rfc you link has been passed allowing for type hints to be _widened_, > that is, that declaring an untyped variable could pass a typed variable, whereas what you are doing > is the inverse. As a picture is worth a thousand words, here is an illustration: Current situation ----------------- interface SeekableIterator extends Iterator { function seek($position); } class LegacyFoo implements SeekableIterator { // (implementation of Iterator elided) function seek($position) { } // ok } class FutureFoo implements SeekableIterator { // (implementation of Iterator elided) function seek(int $position) { } // Error } Proposed change --------------- interface SeekableIterator extends Iterator { function seek(int $position); } class LegacyFoo implements SeekableIterator { // (implementation of Iterator elided) function seek($position) { } // ok (since RFC implemented) } class FutureFoo implements SeekableIterator { // (implementation of Iterator elided) function seek(int $position) { } // ok } Previous Comments: ------------------------------------------------------------------------ [2019-01-24 19:49:50] fractalesque at gmail dot com Hello, unless I am mistaken, the rfc you link has been passed allowing for type hints to be _widened_, that is, that declaring an untyped variable could pass a typed variable, whereas what you are doing is the inverse. That is, if the interface was typed with int, you could implement it without the int, but not add the int where the interface declares untyped. ------------------------------------------------------------------------ [2019-01-24 19:11:01] claude dot pache at gmail dot com Description: ------------ This is related to Bug #71051, but since https://wiki.php.net/rfc/parameter-no-type-variance has been implemented, it should be fixable in PHP7.2+ without breaking legacy code. Per documentation, the signature of SeekableIterator::seek() is: abstract public SeekableIterator::seek ( int $position ) : void However, when implementing the interface, providing the typehint ”int” triggers a compile error. Test script: --------------- class Foo implements SeekableIterator { function current() { } function key() { } function next() { } function rewind() { } function valid() { } function seek(int $position) { } } Expected result: ---------------- No error. Actual result: -------------- Fatal error: Declaration of Foo::seek(int $position) must be compatible with SeekableIterator::seek($position) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=77518&edit=1

« previous php.bugs (#219184) next »