带有问题的 ArrayAccess 演练

发布于 2024-11-15 01:33:53 字数 1322 浏览 1 评论 0原文

我对在 PHP 中实现 ArrayAccess 的实现有一些疑问。

下面是示例代码:

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;
    }
}

问题:

  1. 我不是问为什么我们必须实现ArrayAccess,因为我假设它是 PHP 引擎识别并调用已实现的继承的特殊接口。自动功能?
  2. 为什么我们要公开实现的功能?因为我假设它们是自动调用的特殊函数。它们不应该是私有的,因为在调用 $obj["two"] 时,函数不会从外部调用。
  3. 在 __constructor 函数中分配填充数组是否有特殊原因?这是我知道的构造函数,但在这种情况下它有什么样的帮助。
  4. ArrayAccessArrayObject 之间有什么区别?我在想我通过继承 ArrayAccess 实现的类不支持迭代?
  5. 我们如何在不实现 ArrayAccess 的情况下实现对象索引?

谢谢...

I have some questions about the implementation of implementing ArrayAccess in PHP.

Here is the sample code:

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;
    }
}

Questions:

  1. I am not asking why we do have to implement ArrayAccess since I am assuming it is special interface that PHP Engine recognizes and calls the implemented inherited functions automatically?
  2. Why are we declaring the implemented function public? Since I assume they are special functions called automatically. Shouldn't they be private since when calling saying $obj["two"] the functions are not be called from outside.
  3. Is there a special reason to assign the filled-array in __constructor function? This is the constructor function I know but in this case what kind of help it is being of.
  4. What's the difference between ArrayAccess and ArrayObject? I am thinking the class I implemented by inheriting the ArrayAccess doesn't support iteration?
  5. How could we implement object-indexing without implementing ArrayAccess?

Thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落成白 2024-11-22 01:33:53
  1. 正确
  2. 因为 接口 将它们定义为公共,因此您也必须这样
  3. 做如果您不想,则不必以这种方式编写构造函数*
  4. ArrayAccess 是一个 接口, ArrayObject 是一个类(它本身实现 ArrayAccess
  5. 据我所知没有其他方式

* 您的构造函数可能如下所示

public function __construct( array $data ) {
    $this->container = $data;
}
  1. Correct
  2. Because the interface defines them as public, therefore you have to also
  3. You don't have to write the constructor that way if you don't want to*
  4. ArrayAccess is an interface, ArrayObject is a class (which itself implements ArrayAccess)
  5. No other way that I'm aware of

* Your constructor could look like this

public function __construct( array $data ) {
    $this->container = $data;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文