|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2019-01-24 19:11 UTC] 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) PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 19:00:01 2025 UTC |
> 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 }