歡迎來到小豬圈!

2009-08-11

Array 、 ArrayAccess 與 ArrayObject

  • PHP5 有提供 ArrayAccess Interface 讓自製的 Classes/Objects 也有如陣列 (Array) 型別用 [] 取存方式。 SPL 也有實作 ArrayObject 。
  • 但有一個關鍵的差異是新增陣列項目,一般陣列在無鍵值時是新增一個鍵值為最大整數的項目,鍵值為 null 時則新增一個鍵值為空字串的項目,自製物件是分不出兩者的差異的(無鍵值與 null 都視為 null)。請看下面範例:
    
    <?php
    class ArrayAccessObject implements ArrayAccess{
      private $position 0;
      private $_array = array();
    
      /* impements ArrayAccess */
      public function offsetSet($offset$value) {
        var_dump($offset);
        $this->_array[$offset] = $value;
      }
      public function offsetExists($offset) {
        return isset($this->_array[$offset]);
      }
      public function offsetUnset($offset) {
        unset($this->_array[$offset]);
      }
      public function offsetGet($offset) {
        return isset($this->_array[$offset]) ? $this->_array[$offset] : null;
      }
    }
    
    $a = array();
    $b = new ArrayObject();
    $c = new ArrayAccessObject();
    
    $a[] = 'nothing';
    $b[] = 'nothing';
    $c[] = 'nothing';
    
    $a[null] = 'null';
    $b[null] = 'null';
    $c[null] = 'null';
    
    
    var_dump($a);
    var_dump($b);
    var_dump($c);
    
    /* output */
    
    NULL
    NULL
    array(2) {
      [0]=>
      string(7"nothing"
      [""]=>
      string(4"null"
    }
    object(ArrayObject)#1 (1) {
      ["storage":"ArrayObject":private]=>
      array(2) {
        [0]=>
        string(7"nothing"
        [1]=>
        string(4"null"
      }
    }
    object(ArrayAccessObject)#2 (2) {
      ["position":"ArrayAccessObject":private]=>
      int(0)
      ["_array":"ArrayAccessObject":private]=>
      array(1) {
        [""]=>
        string(4"null"
      }
    }
    
    
    

2009-08-03

base_convert 的限制

  • 剛稍微測試 PHP 函數 base_convert() ,數字約大於 2^54 以上轉換就會出現錯誤。
  • 2009-08-06 更新:數字超過十進位的 9,007,199,254,740,992 即 2^53 以上,轉換就會出現錯誤。
  • 2009-08-09 更新:MySQL 有提供相同功能的函數 CONV(),文件指出最大值為 2^64 (十進位的 18,446,744,073,709,551,615,輸入大於這個值都會傳回相同於這個值的結果),目前測試轉換沒有問題。另外有一點差異是輸出結果為英文大寫字母。

2009-08-01

Apache HTTPD 預設的 .ht* 小技巧

  • httpd.conf 預設都有下列這一段,避免 .ht 開頭(無檔名)的檔案被讀取
    #
    # The following lines prevent .htaccess and .htpasswd files from being 
    # viewed by Web clients. 
    #
    <FilesMatch "^\.ht">
        Order allow,deny
        Deny from all
        Satisfy All
    </FilesMatch>
  • 雖然從網址無法存取,但依然可以從程式讀取。利用這一點,可以將設定檔取為 .htconfig 之類的。

標籤分類

Blog Archive

Labels

Google Analytics Tracking Code

About Me

My photo
Keelung, R.O.C, Taiwan
一個不學無術、混吃等死的傢伙…