0

我有一个扩展 SplFileObject 并实现__toString方法的类,但我似乎得到了不一致的行为,具体取决于方法的调用方式。有时调用父方法,有时调用子方法。谁能解释为什么会这样?

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'
4

1 回答 1

2

这似乎是旧行为。

在 php 5.4+ 中进行测试会产生预期的行为,而 5.3 会产生您所看到的

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

看似相关的错误:
https ://bugs.php.net/bug.php?id=26962
https://bugs.php.net/bug.php?id=60452

于 2013-01-21T00:08:51.780 回答