>php教程 >php手册 >PHP 高级编程(5/5)

PHP 高级编程(5/5)

WBOY
WBOY원래의
2016-06-13 09:33:081100검색

ArrayAccess接口

ArrayAccess接口是对象的行为看起来像个数组,定义了四个方法。接口概要如下:

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

ArrayAccess接口自身没有提供计算书组重元素数量的功能,如果要计算数量可以通过实现Countble接口。这个接口包含了一个count()方法,并且返回元素的数量。

<?<span>php

</span><span>class</span><span> MyArray implements ArrayAccess
{
    </span><span>protected</span><span> $_arr;
    
    </span><span>public</span><span> function __construct()
    {
        $</span><span>this</span>->_arr =<span> array();
    }

    </span><span>public</span><span> function offsetSet($offset, $value)
    {
        $</span><span>this</span>->_arr[$offset] =<span> $value;
    }

    </span><span>public</span><span> function offsetGet($offset)
    {
        </span><span>return</span> $<span>this</span>-><span>_arr[$offset];
    }


    </span><span>public</span><span> function offsetExists($offset)
    {
        </span><span>return</span> array_key_exists($offset, $<span>this</span>-><span>_arr);
    }

    </span><span>public</span><span> function offsetUnset($offset)
    {
        unset($</span><span>this</span>-><span>_arr[$offset]);
    }
}

$MyArray </span>= <span>new</span><span> MyArray();
$MyArray[</span><span>'</span><span>first</span><span>'</span>] = <span>'</span><span>test</span><span>'</span><span>;
echo $MyArray[</span><span>'</span><span>first</span><span>'</span><span>];
unset($MyArray[</span><span>'</span><span>first</span><span>'</span><span>]);

</span>?>

ArratObject 类介绍

ArrayObject 类是一个 ArrayAccess 接口的实现类,它提供了迭代功能,以及很多用来排序和处理数据的非常有用的方法。

ArrayObject <span>implements</span> IteratorAggregate , ArrayAccess , Serializable ,<span> Countable {
</span><span>/*</span><span> Constants </span><span>*/</span>
<span>const</span> <span>integer</span> STD_PROP_LIST = 1<span> ;
</span><span>const</span> <span>integer</span> ARRAY_AS_PROPS = 2<span> ;
</span><span>/*</span><span> Methods </span><span>*/</span>
<span>public</span> __construct ([ <span>mixed</span> <span>$input</span> = [] [, int <span>$flags</span> = 0 [, <span>string</span> <span>$iterator_class</span> = "ArrayIterator"<span> ]]] )
</span><span>public</span> void append ( <span>mixed</span> <span>$value</span><span> )
</span><span>public</span> void <span>asort</span><span> ( void )
</span><span>public</span> int <span>count</span><span> ( void )
</span><span>public</span> <span>array</span> exchangeArray ( <span>mixed</span> <span>$input</span><span> )
</span><span>public</span> <span>array</span><span> getArrayCopy ( void )
</span><span>public</span><span> int getFlags ( void )
</span><span>public</span><span> ArrayIterator getIterator ( void )
</span><span>public</span> <span>string</span><span> getIteratorClass ( void )
</span><span>public</span> void <span>ksort</span><span> ( void )
</span><span>public</span> void <span>natcasesort</span><span> ( void )
</span><span>public</span> void <span>natsort</span><span> ( void )
</span><span>public</span> bool offsetExists ( <span>mixed</span> <span>$index</span><span> )
</span><span>public</span> <span>mixed</span> offsetGet ( <span>mixed</span> <span>$index</span><span> )
</span><span>public</span> void offsetSet ( <span>mixed</span> <span>$index</span> , <span>mixed</span> <span>$newval</span><span> )
</span><span>public</span> void offsetUnset ( <span>mixed</span> <span>$index</span><span> )
</span><span>public</span> <span>string</span> <span>serialize</span><span> ( void )
</span><span>public</span> void setFlags ( int <span>$flags</span><span> )
</span><span>public</span> void setIteratorClass ( <span>string</span> <span>$iterator_class</span><span> )
</span><span>public</span> void <span>uasort</span> ( callable <span>$cmp_function</span><span> )
</span><span>public</span> void <span>uksort</span> ( callable <span>$cmp_function</span><span> )
</span><span>public</span> void <span>unserialize</span> ( <span>string</span> <span>$serialized</span><span> )
}</span>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.