类:

class ToString {

}

直接输出类:

$tostring = new ToString;
echo $tostring;

结果:

Catchable fatal error: Object of class ToString could not be converted to string in 

使用 __tostring方法后的类:

class ToString {

  public function __tostring()
  {
      return 'this class is tostring test';
  }

}

结果:

this class is tostring test

对于日志和错误报告,__tostring方法非常的实用。。