[討論] 能不能這樣判實體化成功與否?

看板PHP作者 (夏天到了,冷不起來了說)時間11年前 (2012/09/26 14:59), 編輯推噓0(003)
留言3則, 1人參與, 最新討論串1/4 (看更多)
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

09/26 15:05, , 1F
好像少了個) =好像應該是==? __toString裡面return ""呢?
09/26 15:05, 1F

09/26 15:07, , 2F
照理說__toString應該不是必要,可能有那個動作觸發了
09/26 15:07, 2F

09/26 15:07, , 3F
把instance轉型成字串的動作
09/26 15:07, 3F
文章代碼(AID): #1GOgVPxv (PHP)
討論串 (同標題文章)
文章代碼(AID): #1GOgVPxv (PHP)