<?php
class TestClass
{
private $a;
private $b;
private $c;
public function __construct($a, $b, $c){
$this->a = $a;
$this->b = $b;
$this->c = $c;
}
public function __toString(){
return $this->a.' '.$this->b.' '.$this->c;
}
}
$class = new TestClass('Good', 'morning,', 'Sir!');
echo $class;
?>
/*
在php5中,可以爲類實現一個名爲__toString()的方法以將類轉換爲一個字符串。
當嘗試打印實現了該方法的類的實例時,__toString()方法被自動調用。
在php 5.2.0之前,__toString()只在實例直接被echo() 和 print()輸出時被調用
*/