Home > Backend Development > PHP Tutorial > PHP Advanced Programming (5/5)_PHP Tutorial

PHP Advanced Programming (5/5)_PHP Tutorial

WBOY
Release: 2016-07-13 10:27:38
Original
971 people have browsed it

ArrayAccess interface

The ArrayAccess interface is an object that behaves like an array and defines four methods. The interface summary is as follows:

<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>
Copy after login

The ArrayAccess interface itself does not provide the function of calculating the number of heavy elements in a book group. If you want to calculate the number, you can implement the Countble interface. This interface contains a count() method and returns the number of elements.

<?<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>?>
Copy after login

Introduction to ArratObject class

The ArrayObject class is an implementation class of the ArrayAccess interface. It provides iteration functions and many very useful methods for sorting and processing data.

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>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/815979.htmlTechArticleArrayAccess interface The ArrayAccess interface is an object that behaves like an array and defines four methods. The interface summary is as follows: ArrayAccess { /* Methods */ abstract public boolean offset...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template