jjok
jjok

Reputation: 619

SplFileObject __toString inheritance

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'

Upvotes: 0

Views: 311

Answers (1)

goat
goat

Reputation: 31823

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

Upvotes: 2

Related Questions