Thanks for the confirmation, it is nice to know that it isn't me just being crazy.
I haven't tried the toString method on any of the other spl objects so I gave it a try by simply adding it to the ProjectDirectory objec in the first example.
class ProjectDirectory extends DirectoryIterator {
public function __construct($path) {
parent::__construct($path);
}
public function current() {
$thisFile = parent::current()->__toString();
return new ProjectFile( $thisFile );
}
public function __toString() {
return "This is the project directory";
}
}
class ProjectFile extends SplFileInfo {
public $fileName;
public function __construct( $fileName ) {
$this->fileName = $fileName;
parent::__construct( $fileName );
}
// fails
function __toString() {
$fileToString = "<a href='$hrefPath". $this->fileName ."'>". $this->fileName ."<a/> [". $this->get_md5() ."]<br />";
return $fileToString;
}
// overloaded but works
public function isWritable() {
return "no writing allowed";
}
// new and works
public function get_md5() {
return md5( file_get_contents( $this->fileName ));
}
}
$directories = "";
$files = "";
$thisDir = new ProjectDirectory( "./" );
echo $thisDir ."<br />";
foreach ( $thisDir as $thisFile ) {
if ( $thisFile->isDir() ) {
$directories .= $thisFile;
}
else {
$files .= $thisFile ."<br />";
}
}
Its output:
.
Files
This is the project directory
This is the project directory
This is the project directory
This is the project directory
This is the project directory
The period would be the current directory, and the This is the project directory is for every single file in that directory.
Just to keep me sane, I changed:
$files .= $thisFile ."<br />";
to
$files .= get_class($thisFile) ." - ". $thisFile ."<br />";
and the output.
ProjectFile - This is the project directory
ProjectFile - This is the project directory
ProjectFile - This is the project directory
ProjectFile - This is the project directory
ProjectFile - This is the project directory
:eek: ProjectFile is the object type, yet it is calling the ProjectDirectory's __toString method? 😕
This is most definitely not the output I am expecting.