Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts

    How does Iterator work?

    I was looking here:
    PHP5 Pagination
    and I saw: Iterator $itorStack. What is it/what does it do?

  • #2
    Regular Coder
    Join Date
    Mar 2007
    Location
    Quebec
    Posts
    261
    Thanks
    6
    Thanked 7 Times in 7 Posts
    I can tell you it is for object-orientated program, but that's all I know. I don't know how to explain it, but the PHP manual has a section for it.
    http://ca3.php.net/iterator

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Iterator is an access interface which defines how an object should be handled with the use of foreach and other looping mechanisms. The one that bugs me is that foreach works on an array, but an array is primitive meaning its not really of type iterable.

    In this specific example, it typehints PHP to require a typeof Iterator for use as an argument when constructing the object of type Pagination.

    Interfaces are one of the most common items overlooked new OO programmers. It develops a contract that can force objects to be used in certain circumstances. A very common interface in most languages (not PHP, lol) is a Comparator or Comparable iterator of some sorts. This contracts an object to have a method to compare to another object similar to itself, and return a result in a numerical fashion (+, 0, -) and is generally useful in sorting algorithms. Once an object is known to be of type Comparable, we know that we can invoke a method compareTo (or similar name) on it. For this reason, I really hope that PHP develops generics in the near future (so I can typecast a typeof Comparable as a requirement and implement Comparable<MyObject> on a class).

    You'll notice two other interfaces used in my pagination object, one is Countable, and the other is SeekableIterator. Countable is used to list the number of items in a collection (with a count($list) or $list->count() fashion), and seekable lets use choose a specific location within a collection. You'll also notice that they are not theoretically required either, as both counting and seeking can be performed on an iterable object without the use of either count or seek but by use of iteration instead.

    I actually mention in I think its the second post in that thread that I thought I was being sneaky with using Iterable, but it ended up causing more trouble than it was worth. Came out good in the long run though
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    Ok, now I'm more confused.
    So can you show me an example of how the iterator $itorStack works and why you used instanceof Countable and instanceof SeekableIterator?

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by Pykex View Post
    Ok, now I'm more confused.
    So can you show me an example of how the iterator $itorStack works and why you used instanceof Countable and instanceof SeekableIterator?
    Yeah:
    PHP Code:
    $aValues = array('Cat''Dog''m' => 'Mouse'); // Arrays are for some reason iterable, but are not of type iterator
    $itorStack = new ArrayObject($aValues); // ArrayObject is of type Iterator

    foreach ($itorStack AS $stackKey => $stackVal
    Simple as that. Since $itorStack is an object of type Iterator, I can use it to break apart key/value combinations from the object using foreach. What the values are depend on what the Iterator key() and current() methods return. This is the purpose of iterator. I'm guessing its supposed to work with current, next, rewind etc:
    PHP Code:
    while ($cur current($itorStack))
    {
        echo 
    $cur;
        
    next($itorStack);

    but it does not work correctly once you've started adding multiple keys and dimensions. So I'm thinking thats a bug.

    instanceof keyword simply returns a boolean that confirms and object is of type x (except in PHP5.0, where it constructed a new object of type x and compared their signatures. Stupid).
    Countable and seekableIterator are used to shortcut the functionality of the methods. With the use of countable, you don't need to iterate every object to count how many are available. You could override the countable method in any given collection object to return a number representing itself. This could be hard coded depending on functionality. It would be exploited if you wanted to extend the MySQLPagination to create the LimitedMySQL Pagination (since the count doesn't reflect the collection).
    Seekable is the same idea. We provide an item we want to seek to (like the fourth) and we forward the record pointer to the fourth value. Otherwise we'll need to seek one by one to find the fourth record.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #6
    eak
    eak is offline
    Regular Coder eak's Avatar
    Join Date
    Jun 2002
    Location
    Nashville, TN
    Posts
    354
    Thanks
    0
    Thanked 26 Times in 26 Posts
    You may also want to take a look at the SPL documentation.

    http://www.php.net/~helly/php/ext/spl/

    It lists all the methods of the Iterator classes/interfaces.
    eak | "Doing a good deed is like wetting your pants; every one can see the results, but only you can feel the warmth."

  • #7
    New Coder
    Join Date
    Dec 2008
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post
    Iterators are used on the vector arrays on most of the languages. With the help of Iterators you can expand the objects functionality on PHP. Build an Interface first with what you want to declare,

    eg; <?php // $array = ?. // initialize the array
    $iterator = new Iterator($array);
    ....
    ...
    ?>

    After declaring an interface, you can implement the declarad interface.

    Object Implement

    eg; <?php
    class ObjectIterator extends Iterator
    {
    var $_objectName;
    <--Constructor--->
    }
    ?>

    The main advantage of this (iterator)function is, it wont make your application go slow and is easy for future developments.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •