Interface usage of arrayAccess, arrayaccess interface_PHP tutorial

WBOY
Release: 2016-07-13 10:04:35
Original
760 people have browsed it

arrayAccess interface usage, arrayaccess interface

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/965028.htmlTechArticlearrayAccess interface usage, arrayaccess interface? php // get the methods instance of ArrayAccess //get the properties instance of ArrayAccess $reflection = new ReflectionClass('Arra...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!