tlw
01-17-2007, 05:26 PM
I am trying to overload the __toString method to print a slightly different output from what the SplFileObject class normally prints.
Unfortunately it isn't working.
Here is all of the code for my program:
class ProjectDirectory extends DirectoryIterator {
public function __construct($path) {
parent::__construct($path);
}
public function current() {
$thisFile = parent::current()->__toString();
return new ProjectFile( $thisFile );
}
}
class ProjectFile extends SplFileInfo {
public $fileName;
public function __construct( $fileName ) {
$this->fileName = $fileName;
parent::__construct( $fileName );
}
// fails
function __toString() {
$fileToString = "<a href='$href_path/". $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( "./" );
foreach ( $thisDir as $thisFile ) {
if ( $thisFile->isDir() ) {
$directories .= $thisFile;
}
else {
$files .= $thisFile ."<br />";
}
}
I have written numerous scripts to test that the __toString method will overload and time and time again was proven successful.
Take this code for example:
class a1 {
public function __toString() {
return "a1";
}
}
class a2 extends a1 {
public function a() {
return new a3();
}
public function __toString() {
return "a2";
}
}
class a3 extends a1 {
public function __toString() {
return "a3";
}
}
$a1 = new a1();
echo $a1 ."<br />";
$a2 = new a2();
echo $a2 ."<br />";
$b = $a2->a();
echo $b ."<br />";
it works just fine and outputs:
a1
a2
a3
Does anybody know what is wrong with the above code?
Unfortunately it isn't working.
Here is all of the code for my program:
class ProjectDirectory extends DirectoryIterator {
public function __construct($path) {
parent::__construct($path);
}
public function current() {
$thisFile = parent::current()->__toString();
return new ProjectFile( $thisFile );
}
}
class ProjectFile extends SplFileInfo {
public $fileName;
public function __construct( $fileName ) {
$this->fileName = $fileName;
parent::__construct( $fileName );
}
// fails
function __toString() {
$fileToString = "<a href='$href_path/". $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( "./" );
foreach ( $thisDir as $thisFile ) {
if ( $thisFile->isDir() ) {
$directories .= $thisFile;
}
else {
$files .= $thisFile ."<br />";
}
}
I have written numerous scripts to test that the __toString method will overload and time and time again was proven successful.
Take this code for example:
class a1 {
public function __toString() {
return "a1";
}
}
class a2 extends a1 {
public function a() {
return new a3();
}
public function __toString() {
return "a2";
}
}
class a3 extends a1 {
public function __toString() {
return "a3";
}
}
$a1 = new a1();
echo $a1 ."<br />";
$a2 = new a2();
echo $a2 ."<br />";
$b = $a2->a();
echo $b ."<br />";
it works just fine and outputs:
a1
a2
a3
Does anybody know what is wrong with the above code?