php / php/php-src

Objects implementing ArrayAccess cannot discern appended values or setting a value with a NULL offset.

Đang mở
#13,992 2 bình luận 0 reaction 1 người được giao Xem trên GitHub

@Girgias đang làm issue này rồi.

Từ ngày 17/4/2024.

Feature Status: Verified
Ngôn ngữ chính
C
Star
40.4k
Fork
8.2k
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
96

Mô tả

Description

While arrays treat $arr[] = 1 and $arr[NULL] = 1 as different operations, with the latter key being juggled to "", ArrayAccess::offsetSet() can only be passed NULL as the key in both cases, leaving the implementer to choose to either append the value to the array or set the "NULL" index.

The following code: https://3v4l.org/KIt91

<?php

class AATest implements ArrayAccess {
    protected $array = [];
    
    // ArrayAccess interface
    public function offsetExists(mixed $offset): bool {
        return key_exists($offset, $this->array);
    }
    public function offsetGet(mixed $offset): mixed {
        return $this->array[$offset];
    }
    public function offsetSet(mixed $offset, mixed $value): void {
        if( is_null($offset) ) {
            $this->array[] = $value;
        } else {
            $this->array[$offset] = $value;
        }
    }
    public function offsetUnset(mixed $offset): void {
        unset($this->array[$offset]);
    }
    
    // debug
    public function dbgGetArray() {
        return $this->array;
    }
}

$object = new AATest();
$object[] = 'a';
$object[NULL] = 'b';
$object[] = 'c';

var_dump($object->dbgGetArray());

$array = [];
$array[] = 'a';
$array[NULL] = 'b';
$array[] = 'c';

var_dump($array);

Resulted in this output:

array(3) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
}
array(3) {
  [0]=>
  string(1) "a"
  [""]=>
  string(1) "b"
  [1]=>
  string(1) "c"
}

But I expected this output instead:

array(3) {
  [0]=>
  string(1) "a"
  [""]=>
  string(1) "b"
  [1]=>
  string(1) "c"
}
array(3) {
  [0]=>
  string(1) "a"
  [""]=>
  string(1) "b"
  [1]=>
  string(1) "c"
}

With a few tweaks you can make 3v4l show that this has been the case all the way back to 5.0.0: https://3v4l.org/OC9qQ

I believe that in order to address this discrepancy there should be another method added to ArrayAccess specifically to handle an append, eg: ArrayAccess::appendValue(mixed $value): void, which would be invoked in the case of $obj[] = 1.

Of course, this would be a BC break, but the prior functionality would be easily restored via defining:

public function appendValue(mixed $value): void {
    $this->offsetSet(NULL, $value);
}

Alternately, NULL could simply be disallowed as a valid array offset entirely. Another BC break, but fixable with $array[$maybeNull ?: ""].

PHP Version

PHP >= 5.0.0

Operating System

No response

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.