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: 2014-03-27 01:31 UTC
Votes:4
Avg. Score:5.0 ± 0.0
Reproduced:4 of 4 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: nikic@php.net Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 5.4.0beta2 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed Sep 18 11:01:30 2019 UTC