PHP編碼二三事兒
自動加載的陷阱
class_exists缺省情況下會觸發autoload,如果你沒注意到這點的話很可能會吃虧,演示代碼:
spl_autoload_register(function($name) { echo $name; }); class_exists('foo');
你可以通過in_array($name, get_declared_classes())函數來判斷是否存在相關的class,這樣不會觸發autoload,不過稍顯笨重,其實class_exists()函數本身可以不觸發autoload,方法是第二個參數:class_exists('foo', false);,不過老實說,當初設計的時候缺省值是true實在是個錯誤的決定。
BTW:method_exists也要注意,不過它沒有類似class_exists那樣能關閉autoload的參數控制,這一點在手冊里已經明確寫出來了,需要注意:
Note: Using this function will use any registered autoloaders if the class is not already known.
所以如果你不想觸發autoload,那么在使用method_exists之前,必須確保對應的類已經加載,否則就沒戲了。
緩存代碼的重復味道
緩存在Web程序里必不可少,最常見的形式如下:
02 {
03 public function find_by_a()
04 {
05 $result = $this->cache->get('cache_a');
06
07 if (!$result) {
08 $result = $this->db->getAll('select ... from ... where a ...');
09
10 $this->cache->set('cache_a', $result);
11 }
12
13 return $result;
14 }
15
16 public function find_by_b()
17 {
18 $result = $this->cache->get('cache_b');
19
20 if (!$result) {
21 $result = $this->db->getAll('select ... from ... where b ...');
22
23 $this->cache->set('cache_b', $result);
24 }
25
26 return $result;
27 }
28 }
這個代碼很平常,實際情況中,多數人差不多都是這么寫代碼,先用某個鍵在緩存里取一下,如果沒有就從數據庫里實際查詢一次,并且把結果緩存起來,這樣的代碼雖然不夠健壯(沒有捕捉可能存在的異常),不過本身并沒有太大問題,但是若干個方法疊加起來,我們就能明顯的感受到壞味道:重復!不說廢話了哦,直接給出解決方案:
02 {
03 public function getCache($key, $closure)
04 {
05 $result = $this->cache->get($key);
06
07 if (!$result) {
08 $result = $closure();
09
10 $this->cache->set($key, $result);
11 }
12
13 return $result;
14 }
15 }
16
17 class Foo extends DAO
18 {
19 public function find_by_a()
20 {
21 return $this->getCache('cache_a', function() {
22 return $this->db->getAll('select ... from ... where a ...');
23 });
24 }
25
26 public function find_by_b()
27 {
28 return $this->getCache('cache_b', function() {
29 return $this->db->getAll('select ... from ... where b ...');
30 });
31 }
32 }
代碼有點簡陋,通過把非公共代碼提取成一個closure,傳遞給getCache方法,從而消除了重復的壞味道。