class ObjectIterator implements Iterator {

   private $obj;
   private $count;
   private $currentIndex;

   function __construct($obj)
   {
     $this->obj = $obj;
     $this->count = count($this->obj->data);
   }
   function rewind()
   {
     $this->currentIndex = 0;
   }
   function valid()
   {
     return $this->currentIndex < $this->count;
   }
   function key()
   {
     return $this->currentIndex;
   }
   function current()
   {
     return $this->obj->data[$this->currentIndex];
   }
   function next()
   {
     $this->currentIndex++;
   }
}

class Object1 implements IteratorAggregate
{
  public $data = array();

  function __construct($in)
  {
    $this->data = $in;
  }

  function getIterator()
  {
    return new ObjectIterator($this);
  }
}

$myObject = new Object1(array(2, 4, 6, 8, 10));

$myIterator = $myObject->getIterator();
for($myIterator->rewind(); $myIterator->valid(); $myIterator->next())
{
  $key = $myIterator->key();
  $value = $myIterator->current();
  echo $key." => ".$value."<br />";
}

Deprecated: Return type of ObjectIterator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/iterator-pag-190/iterator.php on line 25

Deprecated: Return type of ObjectIterator::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/iterator-pag-190/iterator.php on line 29

Deprecated: Return type of ObjectIterator::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/iterator-pag-190/iterator.php on line 21

Deprecated: Return type of ObjectIterator::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/iterator-pag-190/iterator.php on line 17

Deprecated: Return type of ObjectIterator::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/iterator-pag-190/iterator.php on line 13

Deprecated: Return type of Object1::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/iterator-pag-190/iterator.php on line 44
0 => 2
1 => 4
2 => 6
3 => 8
4 => 10

    See the "Interface Synopsis" near the top of https://www.php.net/manual/en/class.iterator.

    For each of those methods in your OjectIterator class, add the same return type hint as specified in that synopsis. E.g., for the current() method, you want to specify it as "mixed":

        function current(): mixed {
            // rest of function code here
        }
    

      I tried to change to void but it doesn't work
      Deprecated: Return type of ObjectIterator::next(): int should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/iterator-pag-190/iterator.php on line 29

      Deprecated: Return type of ObjectIterator::rewind(): mixed should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/iterator-pag-190/iterator.php on line 13

      class ObjectIterator implements Iterator {
      
         private $obj;
         private $count;
         private $currentIndex;
      
         function __construct($obj)
         {
           $this->obj = $obj;
           $this->count = count($this->obj->data);
         }
         function rewind() :mixed
         {
             return $this->currentIndex = 0;
         }
         function valid():bool
         {
           return $this->currentIndex < $this->count;
         }
         function key() :mixed
         {
           return $this->currentIndex;
         }
         function current() :mixed
         {
           return $this->obj->data[$this->currentIndex];
         }
         function next() :mixed
         {
           return $this->currentIndex++;
         }
      }
      
      class Object1 implements IteratorAggregate
      {
        public $data = array();
      
        function __construct($in)
        {
          $this->data = $in;
        }
      
        function getIterator():Traversable
        {
          return new ObjectIterator($this);
        }
      }
      
      $myObject = new Object1(array(2, 4, 6, 8, 10));
      
      $myIterator = $myObject->getIterator() ;
      for($myIterator->rewind(); $myIterator->valid(); $myIterator->next())
      {
        $key = $myIterator->key();
        $value = $myIterator->current();
        echo $key." => ".$value."<br />";
      }

        Iterator::rewind doesn't return anything. It's type should be void like the error message says, and your override shouldn't say return because it shouldn't be returning anything.

        Same thing for Iterator::next.

          Iterator::rewind doesn't return anything. It's type should be void like the error message says, and your override shouldn't say return because it shouldn't be returning anything.
          Same thing for Iterator::next.

          So, would "don't override Iterator methods" be the nutshell answer?

          dalecosp
          Well, Iterator is just an interface, so its methods need implementing. But, yes, you do have to respect the signatures (here the fact that next and rewind have void return) because they form the contract everything else relies on to know how anything implementing it works. ("Oh, it's an Iterator; that means it has a valid() method I can call and get a bool from...")

          What happened is that @bertrc didn't read the error messages to see what return type should be used for each method and just put mixed (almost) everywhere, and added the extra return statements when that didn't work.

          Write a Reply...