__toString() Magic Method (Was: SPL DirectoryIterator)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Heh, I didn't realize, unitl it was too late, that I mislabeled my subject, I've fixed it.

Jochem Maas wrote:

iMHO its pretty much the last one, but I don't see the problem
with what you have written, ok its a little longer, but you
don't have to know that the magic (see below) occurs in order to
understand the expression - i.e. $oFile->getFilename() is pretty
obvious :-), that nice when you get a call twelve months from now

"can you please....?"

Most of my question was stemming from the fact that I was working with DirectoryIterator for the first time, and thought the filename was the output, not an object. My first attempt at seeing it was working was just to echo $oFile (which, btw, was $sFile at that point). When I went to add in the 'hidden.txt' check, it didn't work as expected.



here you are comparing two things - what is there to say that the string type has precedence (and therefore force $oFile to a string(cue magic))? basically this is very hard to do correctly and so that everyone agrees on how it should work exactly. AFAICT.

only echo & print trigger the magic at this point in time AFAICR.

---

class XYZ
{
function __toString() { return "xyz"; }
}
$x = new XYZ;$a = sprintf("%s",$x);
echo $x,"\n";var_dump($a);print($x);echo"\n";print_r($x);

Ahhh, I realize __toString is still in it's infancy (relatively). I remember first hearing about it awhile back, then attempting to use it in a class I was writing at the time. It wasn't working as expected, and the fact that it was working in the DirectoryIterator caught me off guard.

Thanks for the reponse. Seems like I'll need to further investigate it's uses.

http://www.php.net/oop5.magic

Chris

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux