Home  >  Article  >  Backend Development  >  What is ArrayAccess in php? Introduction to ArrayAccess in php (code example)

What is ArrayAccess in php? Introduction to ArrayAccess in php (code example)

不言
不言forward
2018-11-23 15:45:093084browse

The content of this article is about what is ArrayAccess in php? The introduction of ArrayAccess in php (code example) has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

php provides 6 commonly used predefined interfaces to implement certain specific capabilities. The most commonly used one is ArrayAccess, which is used by popular frameworks like Laravel.

What is ArrayAccess

As stated in the official documentation, it "provides an interface with the ability to access objects like accessing arrays."

It provides 4 interfaces

/**
 * 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);
}

We implement these 4 interfaces, which correspond to the isset, read, set, and unset operations of the array in turn.

What’s the use

The definition is very clear, providing the ability to access objects like accessing arrays. Using it, a class can support both object references and array references.

Code implementation example

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('对象定义不合法');
    }
}

Usage example

$container = new Container();

$container['test'] = function () {
  return 'this is a test';
};

var_dump(isset($container['test']));

echo $container['test'];

unset($container['test']);

The above is the detailed content of What is ArrayAccess in php? Introduction to ArrayAccess in php (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete
Previous article:How to use editplusNext article:How to use editplus