我理解它是如何工作的,但为什么我们会实际使用它呢?
<?php
class cat {
public function __toString() {
return "This is a cat\n";
}
}
$toby = new cat;
print $toby;
?>
Run Code Online (Sandbox Code Playgroud)
这不是这个:
<?php
class cat {
public function random_method() {
echo "This is a cat\n";
}
}
$toby = new cat;
$toby->random_method();
?>
Run Code Online (Sandbox Code Playgroud)
我们不能只使用任何其他公共方法输出任何文本?为什么我们需要像这样的魔术方法?