作者JYHuang (夏天到了,冷不起來了說)
看板PHP
標題[討論] 能不能這樣判實體化成功與否?
時間Wed Sep 26 14:59:34 2012
class test{
public $boo;
public functoin __construct($bool){
$this->bool = $bool;
return $bool;
}
public function __toString(){
return $this->bool;
}
}
if ($bool = new test(true){
echo 'it success';
}
在未加入__toString之前是會說could not be converted to string
加入之後是會說回應
toString() must return a string value
嗯..沒辦法在實體化時判斷是否有成功嗎?
還是真的要勤勞點加個屬性來判斷嗎?
(懶人病發作XD)
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 112.104.145.107
→ buganini:好像少了個) =好像應該是==? __toString裡面return ""呢? 09/26 15:05
→ buganini:照理說__toString應該不是必要,可能有那個動作觸發了 09/26 15:07
→ buganini:把instance轉型成字串的動作 09/26 15:07