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

看板PHP作者 (銀色)時間11年前 (2012/09/26 20:12), 編輯推噓0(0010)
留言10則, 3人參與, 最新討論串4/4 (看更多)
※ 引述《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

09/27 00:25, , 1F
其實是有想到一個方法了
09/27 00:25, 1F

09/27 00:26, , 2F
if ($file = file::($path))
09/27 00:26, 2F

09/27 00:27, , 3F
上面打錯 是 if ($file = file::new_file($path))
09/27 00:27, 3F

09/27 00:27, , 4F
然後 在class 裡 判斷是否實體化了,再決定要不要
09/27 00:27, 4F

09/27 00:28, , 5F
回傳一個實體化的物件,記得有版友這個方法滿..Dirty的XD
09/27 00:28, 5F

09/27 02:20, , 6F
對我今天在路上有想到多裹一層這件事
09/27 02:20, 6F

09/27 02:21, , 7F
還有你說的 dirty 板友(誤)應該就是我… XDDD
09/27 02:21, 7F

09/27 02:23, , 8F
應該是指這篇吧? #1FZOB-Qw
09/27 02:23, 8F

09/27 08:27, , 9F
這個dirty的方法貌似跟 singleton 蠻像的..XD
09/27 08:27, 9F

09/27 11:00, , 10F
樓上說的是因為中間過一層產出 XD 這點很多 DP 都會用到啊
09/27 11:00, 10F
文章代碼(AID): #1GOl4-30 (PHP)
討論串 (同標題文章)
文章代碼(AID): #1GOl4-30 (PHP)