PHP5 OOP編程中的代理與異常(2)_PHP教程

      編輯Tag賺U幣
      教程Tag:暫無(wú)Tag,歡迎添加,賺取U幣!

      推薦:詳細(xì)介紹php5編程中的異常處理
      1 首先是try,catch <?php $path = "D:\\in.txt"; try //檢測(cè)異常 { file_open($path); } catch(Exception $e) //捕獲異常 { echo $e->getMessage(); } function

      為此,你需要修改DBQuery對(duì)象以便包括所有的函數(shù)—它們操作一個(gè)來(lái)自DB對(duì)象的結(jié)果資源。當(dāng)執(zhí)行查詢以調(diào)用DB對(duì)象的相應(yīng)函數(shù)并且返回它的結(jié)果時(shí),你需要使用存儲(chǔ)的結(jié)果。下列函數(shù)將被添加:

      列表2:使用代理擴(kuò)展DBQuery類。

      class DBQuery
      {
       .....

       public function fetch_array()
       {  
        if (! is_resource($this->result)) {
         throw new Exception('Query not executed.');
        }

        return $this->db->fetch_array($this->result);
       }

       public function fetch_row()
       {
        if (! is_resource($this->result)) {
         throw new Exception('Query not executed.');
        }

        return $this->db->fetch_row($this->result);
       }

       public function fetch_assoc()
       {
        if (! is_resource($this->result)) {
         throw new Exception('Query not executed.');
        }

        return $this->db->fetch_assoc($this->result);
       }

       public function fetch_object()
       {
        if (! is_resource($this->result)) {
         throw new Exception('Query not executed.');
        }

        return $this->db->fetch_object($this->result);
       }

       public function num_rows()
       {
        if (! is_resource($this->result)) {
         throw new Exception('Query not executed.');
        }

        return $this->db->num_rows($this->result);
       }
      }

      每個(gè)函數(shù)的實(shí)現(xiàn)相當(dāng)簡(jiǎn)單。它首先進(jìn)行檢查,以確保已經(jīng)執(zhí)行查詢,然后把任務(wù)代理到DB對(duì)象,返回它的結(jié)果就好象它是查詢對(duì)象本身(稱作是基本數(shù)據(jù)庫(kù)函數(shù))一樣。

      分享:Zend Framework 入門(mén)——頁(yè)面布局
      Zend Framework 的頁(yè)面布局模塊——Zend_Layout——既可以跟 MVC 一起使用,也可以單獨(dú)使用。本文只討論與 MVC 一起使用的情況。 1. 布局腳本 在 application/views 下

      來(lái)源:模板無(wú)憂//所屬分類:PHP教程/更新時(shí)間:2008-08-22
      相關(guān)PHP教程