PHP,SPL,ArrayAccess接口接口(PHP, SPL, ArrayAccess Inte

2019-10-16 23:27发布

我想了解背后ArrayAccess接口界面的想法,

我不明白每一种方法是一下,如果这些方法(函数)是“内建”的功能和ArrayAccess接口接口(也“中建”)不仅是“确保”我要实现这些“内置”的方法(函数)

我想知道是什么每个thoes功能与我们的代码“幕后花絮”做的事情。

function offsetSet($offset, $value);
function offsetGet($offset);
function offsetUnset($offset);
function offsetExists($offset);

如果我理解了ArrayAccess是一个内置的接口,包含密封件实现,当我们执行他们,我们只实现内置函数thoes引用,我会很高兴,如果有人可以帮助我得到这个权利。

Answer 1:

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

$foo['bar'] = 42的呼叫offsetSet('bar', 42)

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

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

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

你永远不显式调用功能失调*自己。 当你访问对象的数组它发生含蓄。



Answer 2:

在比较ArrayAccessSimpleXMLElement (一个内部类不执行的话),我很好奇,太。 接口是有据可查的手册中了,所以我想强调在与特定类型的偏移一定的差异。

但首先一个样板示例实现执行一类的ArrayAccess给输出访问时:

/**
 * 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" => '']), ' {}');
    }

}

运行与它的一些用法,例子。 我曾在评论形式留下的笔记。 它应该是相当自我解释:

$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数组的主要不同是,你不仅可以使用整型和字符串作为补偿。



文章来源: PHP, SPL, ArrayAccess Interface