What are the potential pitfalls of implementing the directory tree traversal method within the directory object itself?
Implementing the directory tree traversal method within the directory object itself can lead to tight coupling and violate the single responsibility principle. To solve this, it's better to separate the traversal logic into a separate class or function to keep the directory object focused on its core responsibilities.
class Directory {
private $path;
public function __construct($path) {
$this->path = $path;
}
public function getFiles() {
$traverser = new DirectoryTraverser();
return $traverser->traverse($this->path);
}
}
class DirectoryTraverser {
public function traverse($path) {
$files = [];
// Traversal logic goes here
return $files;
}
}
Related Questions
- e) Welche syntaktischen Regeln gelten für den Zugriff auf Array-Keys innerhalb von doppelten Anführungszeichen in PHP?
- What potential issues can arise when using JavaScript instead of PHP headers for redirection in web development?
- How can absolute paths be used effectively in PHP to include files from different directories?