>  기사  >  백엔드 개발  >  1ArrayAccess(어레이 액세스) 인터페이스

1ArrayAccess(어레이 액세스) 인터페이스

WBOY
WBOY원래의
2016-07-29 08:58:05995검색

객체를 PHP 배열처럼 사용하려면 ArrayAccess 인터페이스를 구현해야 합니다.

ArrayAccess는 인터페이스입니다.
1ArrayAccess(어레이 액세스) 인터페이스

<code><span><span>interface</span><span>ArrayAccess</span>
{</span><span>//标示一个元素是否定义</span><span>public</span><span><span>function</span><span>offsetExists</span><span>(<span>$offset</span>)</span>;</span><span>//返回一个元素值</span><span>public</span><span><span>function</span><span>offsetGet</span><span>(<span>$offset</span>)</span>;</span><span>//为一个元素赋值</span><span>public</span><span><span>function</span><span>offsetSet</span><span>(<span>$offset</span>,<span>$value</span>)</span>;</span><span>//删除一个元素值</span><span>public</span><span><span>function</span><span>offsetUnset</span><span>(<span>$offset</span>)</span>;</span>
}</code>

구현:

<code><span><span>class</span><span>Test</span><span>implements</span><span>ArrayAccess</span>
{</span><span>private</span><span>$elements</span> = <span>array</span>();

    <span>//标示一个元素是否定义</span><span>public</span><span><span>function</span><span>offsetExists</span><span>(<span>$offset</span>)</span>
    {</span><span>return</span><span>isset</span>(<span>$this</span>->elements[<span>$offset</span>]);
    }

    <span>//返回一个元素值</span><span>public</span><span><span>function</span><span>offsetGet</span><span>(<span>$offset</span>)</span>
    {</span><span>return</span><span>$this</span>->elements[<span>$offset</span>];
    }

    <span>//为一个元素赋值</span><span>public</span><span><span>function</span><span>offsetSet</span><span>(<span>$offset</span>,<span>$value</span>)</span>
    {</span><span>$this</span>->elements[<span>$offset</span>] = <span>$value</span>;
    }

    <span>//删除一个元素值</span><span>public</span><span><span>function</span><span>offsetUnset</span><span>(<span>$offset</span>)</span>
    {</span><span>unset</span>(<span>$this</span>->elements[<span>$offset</span>]);
    }
}

<span>$test</span> = <span>new</span> Test();
<span>$test</span>[<span>'test'</span>] = <span>'test'</span>;<span>//自动调用offsetSet</span><span>if</span>(<span>isset</span>(<span>$test</span>[<span>'test'</span>]))<span>//自动调用offsetExists</span>
{
    <span>echo</span><span>$test</span>[<span>'test'</span>];<span>//自动调用offsetGet</span><span>echo</span><span>'<br>'</span>;
    <span>unset</span>(<span>$test</span>[<span>'test'</span>]);<span>//自动调用offsetUnset</span>    var_dump(<span>$test</span>[<span>'test'</span>]);
}</code>

<code><span><span>class</span><span>obj</span><span>implements</span><span>ArrayAccess</span>
{</span><span>private</span><span>$container</span> = <span>array</span>();

    <span>public</span><span><span>function</span><span>__construct</span><span>()</span>
    {</span><span>$this</span>->container = <span>array</span>(
            <span>'one'</span> => <span>1</span>,
            <span>'tow'</span> => <span>2</span>,
            <span>'three'</span> => <span>3</span>,
        );
    }

    <span>public</span><span><span>function</span><span>offsetGet</span><span>(<span>$offset</span>)</span>
    {</span><span>return</span><span>isset</span>(<span>$this</span>->container[<span>$offset</span>]) ? <span>$this</span>->container[<span>$offset</span>] : <span>null</span>;
    }

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

    <span>public</span><span><span>function</span><span>offsetExists</span><span>(<span>$offset</span>)</span>
    {</span><span>return</span><span>isset</span>(<span>$this</span>->container[<span>$offset</span>]);
    }

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

<span>$obj</span> = <span>new</span> obj;

var_dump(<span>isset</span>(<span>$obj</span>[<span>"two"</span>])); <span>// 调用 offsetExists</span>
var_dump(<span>$obj</span>[<span>"two"</span>]); <span>//调用 offsetGet</span><span>unset</span>(<span>$obj</span>[<span>"two"</span>]); <span>//调用 offsetUnset</span>
var_dump(<span>isset</span>(<span>$obj</span>[<span>"two"</span>]));<span>// 调用 offsetExists</span><span>$obj</span>[<span>"two"</span>] = <span>"A value"</span>;  <span>// 调用 offsetSet</span>
var_dump(<span>$obj</span>[<span>"two"</span>]); <span>//调用 offsetGet</span><span>$obj</span>[] = <span>'Append 1'</span>;
<span>$obj</span>[] = <span>'Append 2'</span>;
<span>$obj</span>[] = <span>'Append 3'</span>;
print_r(<span>$obj</span>);
</code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

위 내용은 내용의 측면을 포함하여 1ArrayAccess(어레이 액세스) 인터페이스를 소개합니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.