[PHP-NOTES] note 130673 added to class.iteratoraggregate

[email protected] ("[email protected]") Wed, 04 Mar 2026 15:13:51 +0000
Newsgroups php.notes
Message-ID <[email protected]>
With PHP 8.5, using an object as a backing array for ArrayIterator is deprecated, as it allows violating class constraints and invariants. This means the example code above no longer functions. Luckily, there is an easy fix that only requires changing the return statement in the getIterator method. Below are two options for achieving the same functionality:
<?php
    public function getIterator(): Traversable
    {
        return new ArrayIterator(get_object_vars($this));
    }
?>

OR

<?php
    public function getIterator(): Traversable
    {
        yield from get_object_vars($this);
    }
?>
----
Server IP: 206.189.2.9
Probable Submitter: 67.249.172.83
----
Manual Page -- https://php.net/manual/en/class.iteratoraggregate.php
Edit        -- https://main.php.net/note/edit/130673
Del: integrated  -- https://main.php.net/note/delete/130673/integrated
Del: useless     -- https://main.php.net/note/delete/130673/useless
Del: bad code    -- https://main.php.net/note/delete/130673/bad+code
Del: spam        -- https://main.php.net/note/delete/130673/spam
Del: non-english -- https://main.php.net/note/delete/130673/non-english
Del: in docs     -- https://main.php.net/note/delete/130673/in+docs
Del: other reasons-- https://main.php.net/note/delete/130673
Reject      -- https://main.php.net/note/reject/130673
Search      -- https://main.php.net/manage/user-notes.php