首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ArrayAccess接口

ArrayAccess接口
EN

Stack Overflow用户
提问于 2012-07-05 01:46:28
回答 2查看 2.2K关注 0票数 1

我正在尝试理解ArrayAccess接口背后的想法,

我不明白每个方法是关于什么的,如果那些方法(函数)是“内置”函数,而ArrayAccess接口(也称为“内置”)只是“确保”我将实现那些“内置”方法(函数)。

我试图理解每个thoes函数在“幕后”对我们的代码做了什么。

代码语言:javascript
复制
function offsetSet($offset, $value);
function offsetGet($offset);
function offsetUnset($offset);
function offsetExists($offset);

如果我理解ArrayAccess是一个内置接口,包含要实现的封印,当我们实现它们时,我们只实现对thoes内置函数的引用,如果有人能帮助我解决这个问题,我会很高兴。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-05 01:58:51

如果你实现了这个接口,那么这个对象就像一个数组。例如,如果$foo是实现ArrayAccess的类的实例

$foo['bar'] = 42调用offsetSet('bar', 42)

echo $foo['bar']调用offsetGet('bar')

unset($foo['bar'])调用offsetUnset('bar')

isset($foo['bar'])调用offsetExists('bar')

您永远不会自己显式地调用函数offset*。当您以数组的形式访问对象时,它会隐式地发生。

票数 6
EN

Stack Overflow用户

发布于 2012-11-05 21:42:07

在比较ArrayAccessSimpleXMLElement (一个没有实现它的内部类)时,我也很好奇。这个接口在手册中已经有了很好的文档,所以我想强调一些与偏移类型的具体区别。

但首先是一个实现ArrayAccess的类的样板示例实现,它在访问时提供输出:

代码语言:javascript
复制
/**
 * ArrayAccess Example
 */
class ExampleArrayLikeAccess implements ArrayAccess
{

    /**
     * Whether a offset exists
     *
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
     * @param mixed $offset - An offset to check for.
     * @return boolean true on success or false on failure.
     *
     * The return value will be casted to boolean if non-boolean was returned.
     */
    public function offsetExists($offset) {
        echo "  - offsetExists(", $this->varString($offset),")\n";
    }

    /**
     * Offset to retrieve
     *
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
     * @param mixed $offset The offset to retrieve.
     * @return mixed Can return all value types.
     */
    public function offsetGet($offset) {
        echo "  - offsetGet(", $this->varString($offset),")\n";
    }

    /**
     * Offset to set
     *
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
     * @param mixed $offset The offset to assign the value to.
     * @param mixed $value The value to set.
     * @return void
     */
    public function offsetSet($offset, $value) {
        echo "  - offsetSet(", $this->varString($offset), ", ", $this->varString($value), ")\n";
    }

    /**
     * Offset to unset
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
     * @param mixed $offset  The offset to unset.
     * @return void
     */
    public function offsetUnset($offset) {
        echo "  - offsetUnset(", $this->varString($offset),")\n";
    }

    /**
     * helper to give a variable dump in form of a string
     */
    private function varString($var) {
        ob_start();
        var_dump($var);
        return trim(strtr(ob_get_clean(), ["\n" => '', "\r" => '']), ' {}');
    }

}

使用它运行一些用法示例。我已经以评论的形式留下了笔记。这应该是不言而喻的:

代码语言:javascript
复制
$like = new ExampleArrayLikeAccess();


/* offsetExists */

// indexes/keys that behave similar to PHP arrays:

isset($like[1]);    # integer stay integer
# offsetExists(int(1))

isset($like['1']);  # string like an integer - converted to integer
# offsetExists(int(1))

isset($like['01']); # string unlike an integer - stays string
# offsetExists(string(2) "01")

isset($like[TRUE]); # booleans are converted to integer
# offsetExists(bool(true))

// indexes/keys that differ to PHP arrays:

isset($like[1.1]);     # a float stays a float (double)
# offsetExists(double(1.1))

isset($like[NULL]);    # NULL stays NULL
# offsetExists(NULL)

isset($like[array()]); # array stays array
# offsetExists(array(0))

isset($like[$like]);   # object stays object
# offsetExists(class SxeLikeAccess#2 (0))


/* offsetGet */

// indexes/keys behave the same as with offsetExists:
$like[1];    # offsetGet(int(1))
$like['1'];  # offsetGet(int(1))
$like['01']; # offsetGet(string(2) "01")
// ...


/* offsetSet */

$like[1] = 'value';    # index/key behaves the same as with offsetExists
# offsetSet(int(1), string(5) "value")

$like[] = 'value';     # index/key is NULL
# offsetSet(NULL, string(5) "value")

$like[NULL] = 'value'; # index/key is NULL
# offsetSet(NULL, string(5) "value")


/* offsetUnset */
unset($like[1]);       # index/key behaves the same as with offsetExists
unset($like[NULL]);    # same for NULL

与标准PHP数组的主要区别在于,您不仅可以使用整数和字符串作为偏移量。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11333531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档