Rumah > pembangunan bahagian belakang > tutorial php > PHP实现对象属性按数组方式访问

PHP实现对象属性按数组方式访问

WBOY
Lepaskan: 2016-07-28 08:26:48
asal
1417 orang telah melayarinya

主要思路实现ArrayAccess接口和__get,__set魔术方法

<code><span><span>class</span><span>ArrObject</span><span>implements</span><span>ArrayAccess</span> {</span><span>private</span><span>$_data</span>;
    <span>public</span><span><span>function</span><span>__construct</span><span>(<span>$data</span>)</span>{</span><span>$this</span>->_data = <span>$data</span>;
    }
    <span>public</span><span><span>function</span><span>offsetGet</span><span>(<span>$offset</span>)</span>{</span><span>return</span> (<span>$this</span>->offsetExists(<span>$offset</span>) ? <span>$this</span>->_data[<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>$this</span>->_data[<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>->_data[<span>$offset</span>]);
    }
    <span>public</span><span><span>function</span><span>offsetUnset</span><span>(<span>$offset</span>)</span>{</span><span>if</span>(<span>$this</span>->offsetExists(<span>$offset</span>)){
            <span>unset</span>(<span>$this</span>->_data[<span>$offset</span>]);
        }
    }
    <span>public</span><span><span>function</span><span>__get</span><span>(<span>$offset</span>)</span>{</span><span>return</span> (<span>$this</span>->offsetExists(<span>$offset</span>) ? <span>$this</span>->_data[<span>$offset</span>] : <span>null</span>);
    }
    <span>public</span><span><span>function</span><span>__set</span><span>(<span>$offset</span>, <span>$value</span>)</span>{</span><span>$this</span>->_data[<span>$offset</span>] = <span>$value</span>;
    }
}</code>
Salin selepas log masuk

测试:

<code><span>$data</span>  = <span>array</span>(<span>'a'</span>=><span>'a'</span>,<span>'b'</span>=><span>'b'</span>,<span>'c'</span>=><span>'c'</span>);
<span>$test</span>  = <span>new</span> ArrObject(<span>$data</span>);
<span>echo</span><span>$test</span>[<span>'a'</span>]; <span>//a</span><span>echo</span><span>$test</span>->a;   <span>//a</span></code>
Salin selepas log masuk
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了 PHP实现对象属性按数组方式访问,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan