php 給類變數賦值問題

來源:互聯網
上載者:User
private $pagebarnum=10;$this->pagebarnum  = $array['page_name'];$this->set('pagebarnum ',$array['page_name']);/**     * 設定類中指定變數名的值,如果改變數不屬於這個類,將throw一個exception     * @param unknown $var     * @param unknown $value     */    function set($var,$value){        if(in_array($var, get_object_vars($this))){            $this->$var = $value;        }else{            $this->error(__FUNCTION__,$var." does not belong to PB_Page!");        }    }

$this->pagebarnum = $array['page_name'];和$this->set('pagebarnum ',$array['page_name']); 賦值有什麼區別,明明我已經在類中定義$pagebarnum,我用set去賦值還有這個必要嗎?


回複討論(解決方案)

變數初始化和調整當然有必要,假設你一個頁面需要的參數是10,一個頁面的參數是5,你是做兩個類嗎?

變數初始化和調整當然有必要,假設你一個頁面需要的參數是10,一個頁面的參數是5,你是做兩個類嗎?


執行個體化一個類傳參進去能實現,但是沒搞懂的是直接用$this->pagebarnum = $array['page_name'];和set方法中先檢查變數存不存在有什麼區別?pagebarnum 在我建立類的時候已經定義在去檢查有什麼含義。

$this->pagebarnum = $array['page_name'];
$this->set('pagebarnum',$array['page_name']);
的區別在於:後者不會對 不存在的屬性 pagebarnum 賦值,而前者會對不存在的屬性 pagebarnum 建立一個公有的 pagebarnum 屬性

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.