Iterator
These classes extend the Iterator interface, while also restricting its items to that of a specific type. You can create a new iterator by using the opus iterator CLI command, such as:
apex opus iterator <FILENAME> --item-class <FILENAME_OF_ITEM_CLASS>
apex opus iterator MyShop/CustomerIterator --item-class MyShop/Models/Customer
Using the above example, a new file will be created at /src/MyShop/CustomerIterator.php
, which you can push / nshit items to, loop through with foreach
and while
loops, and so on. However, you can not use array functions such as accessing a specific index of the list. All items within the iterator will be restricted to instances of the App\MyShop\Models\Customer
class.
When creating a new instance of an iterator class, you may optionally define a list of initial items within the constructor, for example:
use App\MyShop\Models\Customer;
$items = [
new Customer(48),
new Customer(83),
new Customer(431)
];
$iter = new Iterator(
items: $items
);