eva asked in 電腦與網際網路程式設計 · 2 decades ago

php物件問題??

class currencies {

var $currencies;

function currencies() {

$this->currencies = array();

$currencies_query = tep_db_query("select code, title, symbol_left, symbol_right, decimal_point, thousands_point, decimal_places, value from currencies" );

while ($currencies = tep_db_fetch_array($currencies_query)) {

$this->currencies[$currencies['code']] = array('title' => $currencies['title'],

'symbol_left' => $currencies['symbol_left'],

'symbol_right' => $currencies['symbol_right'],

'decimal_point' => $currencies['decimal_point'],

'thousands_point' => $currencies['thousands_point'],

'decimal_places' => $currencies['decimal_places'],

'value' => $currencies['value']);

}

}

我想請問一下:

$this->currencies[$currencies['code']] = array('title' => $currencies['title']

這句程式的意思是??

              謝謝

1 Answer

Rating
  • Anonymous
    2 decades ago
    Favorite Answer

    1.首先你要先了解一下$this->currencies這個的意思這是代表要存取本類別的變數currencies這個在class currencies的下一行裡面有宣告。2. 此class有一個成員函數叫function currencies()此成員函數和class同名,即代表此類別一被使用時馬上就會先執行的functionex:$test=new currencies;//<===一經宣告後currencies()這個function馬上就會被執行3.成員函數:currencies()一開始即宣告$this->currencies變數為一array;$this->currencies = array(); 再來看while ($currencies = tep_db_fetch_array($currencies_query)) { 此行是把資料庫的執行結果”陣列”回傳給$currencies 變數(此區域變數$currencies 與$this->currencies名稱太過相近是一個不好的寫法)4.重點在這裡$this->currencies[$currencies['code']] = array('title' => $currencies['title'], 'symbol_left' => $currencies['symbol_left'], 'symbol_right' => $currencies['symbol_right'], 'decimal_point' => $currencies['decimal_point'], 'thousands_point' => $currencies['thousands_point'], 'decimal_places' => $currencies['decimal_places'], 'value' => $currencies['value']); 這是指要將資料庫的回傳值(陣列)儲存到$this->currencies這個陣列裡面也就是說$this->currencies[$currencies['code']] 即代表了後面的陣列假設$currencies['code']值是1的話以後的程式就可以$this->currencies[1]來存取剛剛的陣列依此類推可以存取到更多的陣列(此陣列就是資料庫的回傳值)$this->currencies[2]  $this->currencies[3]...........

Still have questions? Get your answers by asking now.