Maison  >  Article  >  développement back-end  >  arrayAccess的接口使用

arrayAccess的接口使用

WBOY
WBOYoriginal
2016-08-08 09:28:491118parcourir

<span>php
    </span><span>//</span><span>get the methods instance of ArrayAccess
    //get the properties instance of ArrayAccess</span>
    <span>$reflection</span> = <span>new</span> ReflectionClass('ArrayAccess'<span>);
    </span><span>//</span><span>var_dump($reflection->getMethods());
    //var_dump($reflection->getProperties());</span>
    
    <span>class</span> dbTypes <span>implements</span><span> ArrayAccess{
        </span><span>private</span> <span>$dbtypes</span> = <span>array</span><span>();
        
        </span><span>//</span><span>判定是否存在</span>
        <span>public</span> <span>function</span> offsetExists(<span>$offset</span><span>){
            </span><span>return</span> <span>isset</span>(<span>$this</span>->dbtypes[<span>$offset</span>]) ? <span>true</span> : <span>false</span><span>;
        }
        </span><span>//</span><span>获取一个值</span>
        <span>public</span> <span>function</span> offsetGet(<span>$offset</span><span>){
            </span><span>if</span>(<span>$this</span>->offsetExists(<span>$offset</span><span>)){
                </span><span>return</span> <span>$this</span>->dbtypes[<span>$offset</span><span>];
            }</span><span>else</span><span>{
                </span><span>return</span> <span>null</span><span>;
            }
        }
        </span><span>//</span><span>设置一个值</span>
        <span>public</span> <span>function</span> offsetSet(<span>$offset</span>,<span>$value</span><span>){
                </span><span>$this</span>->dbtypes[<span>$offset</span>] = <span>$value</span><span>;
        }
        </span><span>//</span><span>删除一个值</span>
        <span>public</span> <span>function</span> offsetUnset(<span>$offset</span><span>){
            </span><span>unset</span>(<span>$this</span>->dbtypes[<span>$offset</span><span>]);
        }
    }
    
    </span><span>$types</span> = <span>new</span><span> dbTypes();
    </span><span>echo</span> <span>$types</span>['nosql'<span>];
    
    
    
    </span>

以上就介绍了arrayAccess的接口使用,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:php get_class()函数Article suivant:php中get post请求方法封装