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'
有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top