How can the iteration process be separated from the directory object to improve code structure and maintainability?

To separate the iteration process from the directory object, we can create a separate class or function responsible for iterating over the directory contents. This helps improve code structure by separating concerns and makes the code more maintainable as changes to the iteration logic can be made independently of the directory object.

class DirectoryIterator {
    public static function iterateDirectory($directory) {
        $files = scandir($directory);
        
        foreach ($files as $file) {
            if ($file != '.' && $file != '..') {
                echo $file . PHP_EOL;
            }
        }
    }
}

// Usage
$directory = '/path/to/directory';
DirectoryIterator::iterateDirectory($directory);