php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60161 Implementing an interface extending Traversable is order dependent
Submitted: 2011-10-28 18:45 UTC Modified: 2020-02-26 15:49 UTC
Votes:5
Avg. Score:4.8 ± 0.4
Reproduced:5 of 5 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: nikic@php.net Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.4.0beta2 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: nikic@php.net
New email:
PHP Version: OS:

 

 [2011-10-28 18:45 UTC] nikic@php.net
Description:
------------
When implementing an interface which extends Traversable together with one of the concrete Traversable implementations (i.e. Iterator or IteratorAggregate) the latter must be named first in order to not throw an error.

Compare:
http://codepad.viper-7.com/OJt8XN
http://codepad.viper-7.com/1AnU5Z

Responsible code:
http://lxr.php.net/xref/PHP_5_4/Zend/zend_interfaces.c#zend_implement_traversable


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-11-02 12:21 UTC] johannes@php.net
Actually we should forbid extending Traversable. This should only only only be used by internal classes (Iterator, iteratorAggregate or classes providing the C get_iterator hooks)
 [2011-11-02 14:44 UTC] nikic@php.net
@johannes: How could you then hint that a class must be Traversable in some way (either Iterator or Aggregate)? Or shouldn't that be possible at all?
 [2011-11-02 17:16 UTC] johannes@php.net
You mean for a type hint which says "traversable and having that function"?  That might indeed be a valid reason I didn't think about ..
 [2011-11-02 17:50 UTC] nikic@php.net
Yes, exactly. That's what I was trying to do when I encountered the issue.
 [2013-10-12 17:57 UTC] maciej dot sz at gmail dot com
The order dependency of interface names is a minor drawback, but same thing happens when one tries to put an abstract class between the interface and specific implementation. And this is a big problem:

<?php
interface IFoo extends \Traversable
{}

abstract class AFoo implements IFoo
{}
?>

Throws:
Class AFoo must implement interface Traversable as part of either Iterator or IteratorAggregate in Unknown on line 0
 [2014-03-27 01:31 UTC] levim@php.net
I don't care as much about the abstract issue (it's filed separately: https://bugs.php.net/bug.php?id=62609) but I'd really like to see the order restriction be relaxed on interfaces.
 [2016-12-13 00:28 UTC] thecelavi at mac dot com
Fatal: https://3v4l.org/ogZk4
Fatal: https://3v4l.org/9VqNt
Fine: https://3v4l.org/JhECB

Expected: Every example should execute fine.
 [2020-02-26 15:49 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2020-02-26 15:49 UTC] nikic@php.net
Looks like this got fixed in 7.4. Test added in https://github.com/php/php-src/commit/8c8f8c419378822f336aab13f301519cf2b95feb.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 22:01:28 2024 UTC