1ArrayAccess(數組式存取)接口

WBOY
發布: 2016-07-29 08:58:05
原創
998 人瀏覽過

如果想讓物件使用起來像PHP數組,那麼我們需要實作ArrayAccess介面。

ArrayAccess是一個interface,實作這個interface,必須實現以下幾個方法:
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(numbering); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介紹了1ArrayAccess(陣列式存取)接口,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板