PHP 進階編程(5/5)_PHP教程

來源:互聯網
上載者:User
ArrayAccess介面

ArrayAccess介面是對象的行為看起來像個數組,定義了四個方法。介面概要如下:

ArrayAccess {/* Methods */abstract public boolean offsetExists ( mixed $offset )abstract public mixed offsetGet ( mixed $offset )abstract public void offsetSet ( mixed $offset , mixed $value )abstract public void offsetUnset ( mixed $offset )}

ArrayAccess介面自身沒有提供計算書組重元素數量的功能,如果要計算數量可以通過實現Countble介面。這個介面包含了一個count()方法,並且返回元素的數量。

phpclass MyArray implements ArrayAccess{    protected $_arr;        public function __construct()    {        $this->_arr = array();    }    public function offsetSet($offset, $value)    {        $this->_arr[$offset] = $value;    }    public function offsetGet($offset)    {        return $this->_arr[$offset];    }    public function offsetExists($offset)    {        return array_key_exists($offset, $this->_arr);    }    public function offsetUnset($offset)    {        unset($this->_arr[$offset]);    }}$MyArray = new MyArray();$MyArray['first'] = 'test';echo $MyArray['first'];unset($MyArray['first']);?>

ArratObject 類介紹

ArrayObject 類是一個 ArrayAccess 介面的實作類別,它提供了迭代功能,以及很多用來排序和處理資料的非常有用的方法。

ArrayObject implements IteratorAggregate , ArrayAccess , Serializable , Countable {/* Constants */const integer STD_PROP_LIST = 1 ;const integer ARRAY_AS_PROPS = 2 ;/* Methods */public __construct ([ mixed $input = [] [, int $flags = 0 [, string $iterator_class = "ArrayIterator" ]]] )public void append ( mixed $value )public void asort ( void )public int count ( void )public array exchangeArray ( mixed $input )public array getArrayCopy ( void )public int getFlags ( void )public ArrayIterator getIterator ( void )public string getIteratorClass ( void )public void ksort ( void )public void natcasesort ( void )public void natsort ( void )public bool offsetExists ( mixed $index )public mixed offsetGet ( mixed $index )public void offsetSet ( mixed $index , mixed $newval )public void offsetUnset ( mixed $index )public string serialize ( void )public void setFlags ( int $flags )public void setIteratorClass ( string $iterator_class )public void uasort ( callable $cmp_function )public void uksort ( callable $cmp_function )public void unserialize ( string $serialized )}

http://www.bkjia.com/PHPjc/815979.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/815979.htmlTechArticleArrayAccess介面 ArrayAccess介面是對象的行為看起來像個數組,定義了四個方法。介面概要如下: ArrayAccess { /* Methods */ abstract public boolean offse...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.