php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61808 Could not implement interface Iterator
Submitted: 2012-04-22 05:29 UTC Modified: 2012-04-22 10:25 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: nanocaiordo at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.4.0 OS: Fedora 17 x86_64
Private report: No CVE-ID: None
 [2012-04-22 05:29 UTC] nanocaiordo at gmail dot com
Description:
------------
I cannot find anything within changelogs, bug reportings and documentations 
relating to this issue.

It works fine in php 5.3.10, but gives me fatal error in PHP 5.4.

Test script:
---------------
<?php
error_reporting(E_ALL | E_DEPRECATED | E_STRICT);
ini_set('display_errors', 1);

interface Foo_SQL_Interface_Result extends ArrayAccess, Countable, Iterator
{
 public function data_seek($offset);
 public function fetch_all($resulttype=MYSQLI_NUM);
 public function fetch_array();
 public function fetch_assoc();
 public function fetch_field_direct($offset);
 public function fetch_field();
 public function fetch_fields();
 public function fetch_object();
 public function fetch_row();
 public function field_seek($offset);
 public function free();
}

class Foo_MySQLi_UseResult extends MySQLi_Result
{
 function __construct(MySQLi $obj) { parent::__construct($obj); }
 public function fetch_array($type_cast=false) {}
 public function fetch_assoc($type_cast=false) {}
 public function fetch_row($type_cast=false) {}
 public function fetch_all($resulttype=MYSQLI_ASSOC, $type_cast=false) {}
}

class Foo_MySQLi_Result extends Foo_MySQLi_UseResult implements Foo_SQL_Interface_Result
{
 public function data_seek($offset) {}
 public function fetch_field_direct($offset) {}
 public function field_seek($offset) {}
 # ArrayAccess
 public function offsetExists($k)  {}
 public function offsetGet($k)     {}
 public function offsetSet($k, $v) {}
 public function offsetUnset($k)   {}
 # Countable
 public function count()   {}
 # Iterator
 public function key()     {}
 public function current() {}
 public function next()    {}
 public function rewind()  {}
 public function valid()   {}
}


Actual result:
--------------
Fatal error: Class Foo_MySQLi_Result could not implement interface Iterator in 
Unknown on line 0

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-04-22 06:42 UTC] laruence@php.net
as of PHP-5.4, MySQLi_Result implements Traversable, but not sure, whether this 
should be a bug.
 [2012-04-22 07:02 UTC] nanocaiordo at gmail dot com
Good decision indeed.
But a fatal error, even if natural in this case, will make it impossible to run on 
both 5.3.10 and 5.4.

Not sure if in this case a E_WARNING or a E_DEPRECATED could be used instead.

However, I just found it in the changelog as "MySQLi: Added iterator support in 
MySQLi. mysqli_result implements Traversable."

Shouldn't be added somewhere in the "migration" changes.
 [2012-04-22 08:57 UTC] nanocaiordo at gmail dot com
I also noticed few mismatches spread in documentation and or code logic.

###
Quote: http://www.php.net/manual/en/class.traversable.php
"The Traversable interface: Abstract base interface that cannot be implemented alone. Instead it must be implemented by either 
IteratorAggregate or Iterator."

But then, "MySQLi_Result implements Traversable"

###
Quote: http://php.net/manual/en/class.mysqli-result.php
"5.4.0: Iterator support was added, as mysqli_result now implements Traversable."

1. Implementing Traversable doesn't provide Iterator support, instead clashes with it.
2. mysqli_result still doesn't include any of the "Iterator" methods

###
. changes should be reverted.
. or change it to "MySQLi_Result implements Iterator" and add all methods.
. make the Traversable interface be liberally implemented.

Cheers
 [2012-04-22 10:25 UTC] johannes@php.net
-Status: Open +Status: Not a bug
 [2012-04-22 10:25 UTC] johannes@php.net
In internal classes we can do many things more which aren't possible for user. The documentation is for users, and users can't implement traversable.

To the second quote: "iterators" are an concept which can be implemented in different ways. In userspace code one can use the Iterator or IteratorAggregate interface, in C code one can use Traversable.

It was decided to provide iterator support by using Traversale, not Iterator, as the class already provides different ways to work on the result set and adding Iterator's methods creates a more messy interface ...
 [2012-05-11 14:03 UTC] djmaze at cpgnuke dot com
As this might add more problems in future with other php classes written in C, can't there be made any solution to this?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 13:01:29 2024 UTC