Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9232311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:17:03+00:00 2026-06-18T06:17:03+00:00

I developed a interface and class to shield the PDOStatement. The interface: interface ResultSetInterface

  • 0

I developed a interface and class to shield the PDOStatement.

The interface:

interface ResultSetInterface extends Iterator
{
    public function count();
    public function all();
}

The class:

class ResultSet implements ResultSetInterface
{
    /**
     * @var PDOStatement
     */
    protected $pdoStatement;

    protected $cursor = 0;

    protected $current = null;

    private $count = null;

    public function __construct($pdoStatement)
    {
        $this->pdoStatement= $pdoStatement;
        $this->count = $this->pdoStatement->rowCount();
    }

    public function rewind()
    {
        if ($this->cursor > 0) {
            throw new Exception('Rewind is not possible');
        }
        $this->next();
    }

    public function valid()
    {
        return $this->cursor <= $this->count;
    }

    public function next()
    {
        $this->current = $this->pdoStatement->fetch();
        $this->cursor++;
    }

    public function current()
    {
        return $this->current;
    }

    public function key()
    {
    }

    public function count()
    {
        return $this->count;
    }

    public function all() {
        $this->cursor = $this->count();
        return $this->pdoStatement->fetchAll();
    }
}

This works fine. But I’m not sure how to use the key() method which is necessary to implement an Iterator class. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:17:04+00:00Added an answer on June 18, 2026 at 6:17 am

    First of all, about your interface, I think it would be better for you to extend CountableIterator as you want to add the count()method and there is a magical interface for that purpose in SPL.

    About the key method. You have to remember in PHP every iterable content is an association of a key and a value. It is inherited from PHP arrays.

    Iterator is a way to overload the foreachoperator and as foreach as a sythax which is composed as foreach($iterator as $key=>$value) you have to give the key method implementation.

    In your case you have two solutions :

    • using the $pdo->cursor
    • create your own attribute called $currentKey and increment it each time you use nextmethod.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed some classes with similar behavior, they all implement the same interface.
Given the following code: public interface IExample { ... } public abstract class BaseExample:
I've been trying to create a simple class that implements the ViewSwitcher.ViewFactory interface basing
from: https://developer.android.com/reference/android/content/res/XmlResourceParser.html it said that: public interface XmlResourceParser implements AttributeSet XmlPullParser. As it said,
I've developed a basic CXF JAX-WS web service. Some details of web service, interface:
using an interface (abstract class) on c++ i have the need to force any
I have developed a desktop database application. But the interface is not rich. So
For example, (although it's not an interface) the Stream class in .NET has an
Suppose I developed a wrestling game where all wrestlers fight with each-other. So I
Here's a (reduced) class declaration from an example on apple's developer: @interface myController :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.