• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » PHP » PHP Iterator interface
Next →
← Prev

PHP Iterator interface

By Dinesh Thakur

The conditional structure “foreach” when you allowed to browse the contents of a table. With version 5 of PHP, you can now browse an entire object to retrieve its properties.

We’ll be covering the following topics in this tutorial:

  • Reminder of Interfaces
  • The Iterator interface

Reminder of Interfaces

An interface is a way to contract with a class, to ensure that it will have much of certain methods / properties. Unlike inheritance, it is possible to implement multiple interfaces, allowing to overcome the inability to use multiple inheritance in PHP.

The Iterator interface

The native interface Iterator has existed for some time in Java and provides a significant advantage in terms of object oriented programming. Here’s what the code looks like the Iterator interface:

<?php
    Interface Iterator
   {
        public function rewind();
        public function key();
        public function current();
        public function next();
        public function valid();
    }
?>

Browse an object with foreach ()

<?php
    class MyClass {
        protected $arg1 = 'Good' ;
        protected $arg2 = 'Bye';
         //...
     }
      $c = new MyClass();
      foreach($c as $key=>$value) {
      echo $key, ' : ', $value, '<br/>';
   }
?>

This will print:

arg1 : Good
arg2 : Bye

This is where the Iterator interface. She will indeed help you customize the behavior of foreach. Implement this interface in your class requires you to overload (or redefine) the following 5 methods: rewind (), next (), key (), current () and valid ().

<?php
    class MyClass implements Iterator {
      protected $n;
      const MAX = 5;

      public function rewind() {
          $this->n = 0;
       }

      public function next() {
           $this->n++;
       }

       public function key() {
           return 'increment '.$this->n+1;
       }

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

       public function valid() {
            return $this->n<=self::MAX;
       }
}
      $c = new MyClass();
         foreach($c as $key => $val) {
           echo $key,' : ',$val, '<br/>';
        }
?>

This will print:

increment 1: 0
increment 2: 1
Increment 3: 2
increment 4: 3
increment 5: 4

These methods will be called by foreach, in this order:

1. rewind at the first iteration, which allows you to rewind your item (here it gives $ n 0)
2. valid which verifies that one does not arrive at the end of the iteration (where $ n is less than the maximum number of iterations set by the constant MAX). If valid returns TRUE, if it continues we stop, it’s the end of the iteration.
3. which returns the current value of the current iteration (here $ n)
4. key which returns the key of the current iteration (here key. $ n)
5. next that starts the next iteration (here, $ n is incremented by 1)
6. Is recalled after the valid () method that checks again that we did not come under Iteration.

Note [1]: A class can not implement two interfaces that share, however, function names.

You’ll also like:

  1. Iterator Java Example
  2. Introduction to PHP
  3. PHP Cookies
  4. PHP Sessions
  5. PHP Magic Methods: set () and get ()
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

PHP Tutorials

PHP Tutorials

  • PHP - Home
  • PHP - Features
  • PHP - Magic Methods
  • PHP - Imagefilter
  • PHP - Arrays Numeric
  • PHP - Sessions
  • PHP - Forms Processing
  • PHP - clone()
  • PHP - Cookies
  • PHP - Variable Types
  • PHP - First program
  • PHP - call()
  • PHP - Iterator interface
  • PHP - Imports files
  • PHP - Exception Handling
  • PHP - set() and get()
  • PHP - Install MAMP
  • PHP - Functions
  • PHP - Constants Types
  • PHP - Comments Types
  • PHP - OOP's
  • PHP - OOps Use
  • PHP - PHP Code & Redirect 301
  • PHP - Control structures
  • PHP - Abstract Classes
  • PHP - Control structures
  • PHP - Classes
  • PHP - MySQL NULL values
  • PHP - Methods Visibility
  • PHP - Operator Types
  • PHP - Short tags Not use
  • PHP - Object and class
  • PHP - Secure Passwords

Other Links

  • PHP - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW