作者JYHuang (夏天到了,冷不起來了說)
看板PHP
標題Re: [討論] 能不能這樣判實體化成功與否?
時間Wed Sep 26 17:01:07 2012
※ 引述《gpmm (銀色)》之銘言:
: 其實看你的例子還是不很明白你要做什麼 o_Oa"
: 另外如果會有「實體化失敗」的情況那應該是直接噴 error 了吧?
class file{
public $file_path;
public $file_name;
public $success=false;
public functoin __construct($full_name){
if (file_exists($full_name) && is_file($full_name))
$this->success = true;
$this->file_name = basename($full_name);
$this->file_path = dirname($full_name;
}
}
因為我是要視內部條件來決定是否能實體化
看看能不能把下列
$file = new file(file_with_path);
if($file->success){
//dome something
}
簡化變成
if($file = new file(file_with_path){
//dome something
}
這麼懶而以 XDD
--
寫到後來,
有時繞一下其實不會怎麼
卻一直跳脫不出想要看看能不能直線通過的思維 =.=
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 112.104.145.107
→ CaptainH:這不是實體化失敗吧= = 09/26 17:25
→ CaptainH:畢竟這個 object 還是有被 new 出來 09/26 17:26
推 carlcarl:建構式丟exception 然後在呼叫的時候用try catch包起來? 09/26 18:50
推 lausai:或是把new file()包在一個func裡面? 09/26 21:16
→ buganini:funt oi n __construct 09/26 22:43
→ buganini:funct oi n… 我也打錯XD 09/26 22:44
→ JYHuang:打錯是難免的 XD 09/27 00:25