• 技术文章 >后端开发 >php教程

    php中ArrayAccess是什么?php中ArrayAccess的介绍(代码示例)

    不言不言2018-11-23 15:45:09转载1326

    本篇文章给大家带来的内容是关于php中ArrayAccess是什么?php中ArrayAccess的介绍(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

    php提供了6个常用的预定义接口,实现某些特定的能力。其中最最常用的就是 ArrayAccess 了,像 Laravel 这种流行的框架都用到了它。

    ArrayAccess 是啥

    如官方文档所述,它“提供像访问数组一样访问对象的能力的接口”。

    它提供了4个接口

    /**
     * Interface to provide accessing objects as arrays.
     * @link http://php.net/manual/en/class.arrayaccess.php
     */
    interface ArrayAccess {
    
        /**
         * Whether a offset exists
         * @link http://php.net/manual/en/arrayaccess.offsetexists.php
         * @param mixed $offset <p>
         * An offset to check for.
         * </p>
         * @return boolean true on success or false on failure.
         * </p>
         * <p>
         * The return value will be casted to boolean if non-boolean was returned.
         * @since 5.0.0
         */
        public function offsetExists($offset);
    
        /**
         * Offset to retrieve
         * @link http://php.net/manual/en/arrayaccess.offsetget.php
         * @param mixed $offset <p>
         * The offset to retrieve.
         * </p>
         * @return mixed Can return all value types.
         * @since 5.0.0
         */
        public function offsetGet($offset);
    
        /**
         * Offset to set
         * @link http://php.net/manual/en/arrayaccess.offsetset.php
         * @param mixed $offset <p>
         * The offset to assign the value to.
         * </p>
         * @param mixed $value <p>
         * The value to set.
         * </p>
         * @return void
         * @since 5.0.0
         */
        public function offsetSet($offset, $value);
    
        /**
         * Offset to unset
         * @link http://php.net/manual/en/arrayaccess.offsetunset.php
         * @param mixed $offset <p>
         * The offset to unset.
         * </p>
         * @return void
         * @since 5.0.0
         */
        public function offsetUnset($offset);
    }

    我们实现这4个接口,依次对应数组的isset,读取,设置,unset操作。

    有什么用

    定义说的很明白啦,提供像访问数组一样访问对象的能力。用上了它,可以让一个类即可以支持对象引用,也支持数组引用。

    代码实现示例

    class Container implements ArrayAccess
    {
    
        /**
         * @var array 单例对象索引
         */
        private $instances = [];
    
        /**
         * @var array 可实例化对象定义索引
         */
        private $definitions = [];
    
        public function offsetExists($offset)
        {
            return isset($this->definitions[$offset]);
        }
    
        public function offsetGet($offset)
        {
            if (isset($this->instances[$offset])) {
                return $this->instances[$offset];
            } elseif (isset($this->definitions[$offset])) {
                return $this->make($offset);
            }
    
            throw new \Exception('未提供对象定义');
        }
    
        public function offsetSet($offset, $value)
        {
            // ... 省略一些较验判断
            $this->definitions[$offset] = $value;
        }
    
        public function offsetUnset($offset)
        {
            unset($this->definitions[$offset]);
            unset($this->instances[$offset]);
        }
    
        private function make($offset)
        {
            $definition = $this->definitions[$offset];
    
            if ($definition instanceof \Closure) {
                return $this->instances[$offset] = $definition();
            }
    
            if (is_object($definition)) {
                return $this->instances[$offset] = $definition;
            }
    
            if (is_array($definition)) {
                $class = $definition['class'];
                $reflection = new \ReflectionClass($class);
    
                $dependencies = [];
                // ... 省略反射的实现代码
                $object = $reflection->newInstanceArgs($dependencies);
                return $this->instances[$offset] = $object;
            }
    
            throw new \Exception('对象定义不合法');
        }
    }

    使用示例

    $container = new Container();
    
    $container['test'] = function () {
      return 'this is a test';
    };
    
    var_dump(isset($container['test']));
    
    echo $container['test'];
    
    unset($container['test']);

    以上就是php中ArrayAccess是什么?php中ArrayAccess的介绍(代码示例)的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:segmentfault,如有侵犯,请联系admin@php.cn删除
    专题推荐:php 数组
    上一篇:editplus怎么用 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • PHP数组式访问接口 ArrayAccess• PHP - ArrayAccess接口详解• PHP预定义接口之 ArrayAccess• 24php中使用Iterator,ArrayAccess和Countable• ArrayAccess 创建一个类数组对象• 代码详解php中的ArrayAccess接口• PHP数组访问接口ArrayAccess使用详解
    1/1

    PHP中文网