Question

I've got a class that extends SplFileObject and implements a __toString method, but I seem to get inconsistent behaviour, depending on how the method is called. Sometimes the parent method is called, and sometimes the child method. Can anyone explain why this happens?

class Something extends \SplFileObject {
    public function __toString() {
        return 'calling __toString';
    }
}

$something = new Something('test.php');
echo $something; // 'calling __toString'
echo (string) $something; // 'test.php'
echo $something->__toString(); // 'calling __toString'
Was it helpful?

Solution

It seems like old behavior.

Testing in php 5.4+ yields expected behavior, while 5.3 yielded what you saw

http://codepad.viper-7.com/aZgP1h

seemingly related bugs:
https://bugs.php.net/bug.php?id=26962
https://bugs.php.net/bug.php?id=60452

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top