作者gpmm (銀色)
看板PHP
標題Re: [討論] 能不能這樣判實體化成功與否?
時間Wed Sep 26 20:12:44 2012
※ 引述《JYHuang (夏天到了,冷不起來了說)》之銘言:
: ※ 引述《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
: --
: 寫到後來,
: 有時繞一下其實不會怎麼
: 卻一直跳脫不出想要看看能不能直線通過的思維 =.=
嘛…我覺得可能沒辦法 XDD
正要出門沒時間細想,丟一個直覺上的「少一行」解
if ($file = new file ($path) && $file->success) {
// do something
}
--
我路上再想想 XD
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 175.180.182.236
→ JYHuang:其實是有想到一個方法了 09/27 00:25
→ JYHuang:if ($file = file::($path)) 09/27 00:26
→ JYHuang:上面打錯 是 if ($file = file::new_file($path)) 09/27 00:27
→ JYHuang:然後 在class 裡 判斷是否實體化了,再決定要不要 09/27 00:27
→ JYHuang:回傳一個實體化的物件,記得有版友這個方法滿..Dirty的XD 09/27 00:28
→ gpmm:對我今天在路上有想到多裹一層這件事 09/27 02:20
→ gpmm:還有你說的 dirty 板友(誤)應該就是我… XDDD 09/27 02:21
→ gname:這個dirty的方法貌似跟 singleton 蠻像的..XD 09/27 08:27
→ gpmm:樓上說的是因為中間過一層產出 XD 這點很多 DP 都會用到啊 09/27 11:00