zoukankan      html  css  js  c++  java
  • ArrayAccess(数组式访问)接口

    Yii、laravel源代码中,注意到有对象以数组的形式访问数据,即下面原理

    <?php
    class obj implements arrayaccess {
        private $container = array();
        public function __construct() {
            $this->container = array(
                "one"   => 1,
                "two"   => 2,
                "three" => 3,
            );
        }
        public function offsetSet($offset, $value) {
            if (is_null($offset)) {
                $this->container[] = $value;
            } else {
                $this->container[$offset] = $value;
            }
        }
        public function offsetExists($offset) {
            return isset($this->container[$offset]);
        }
        public function offsetUnset($offset) {
            unset($this->container[$offset]);
        }
        public function offsetGet($offset) {
            return isset($this->container[$offset]) ? $this->container[$offset] : null;
        }
    }
    
    $obj = new obj;
    
    var_dump(isset($obj["two"]));
    var_dump($obj["two"]);
    unset($obj["two"]);
    var_dump(isset($obj["two"]));
    $obj["two"] = "A value";
    var_dump($obj["two"]);
    $obj[] = 'Append 1';
    $obj[] = 'Append 2';
    $obj[] = 'Append 3';
    print_r($obj);
    ?>
    
  • 相关阅读:
    android测试点汇总
    Java Web应用调优线程池
    大型网站架构技术一览
    如何用消息系统避免分布式事务
    VMware Tools安装
    Git
    构架分布式队列编程
    排序算法概述
    ThreadLocal使用和原理
    JVM常用参数配置
  • 原文地址:https://www.cnblogs.com/meetuj/p/10344702.html
Copyright © 2011-2022 走看看