Questions tagged [arrayaccess]

Array access is class implementation that allows the class to behave as an array - this means to provide data through [] operator.

57 questions
28
votes
5 answers

In C++11 and beyond does std::string::operator[] do bounds checking?

I have seen many times that std::string::operator[] does not do any bounds checking. Even What is the difference between string::at and string::operator[]?, asked in 2013, the answers say that operator[] does not do any bounds checking. My issue…
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
19
votes
5 answers

Adding integers to arrays in C++?

Consider: int sum(const int numbers[], const int size){ if (size == 0) return 0; else return numbers[0] + sum(numbers+1, size-1); } This is a simple recursive function from MIT 6.096 for adding an arbitrary number of…
18
votes
2 answers

PHP Type hinting to allow Array or ArrayAccess

Is it possible to allow an Array or an object that implements ArrayAccess? For example: class Config implements ArrayAccess { ... } class I_Use_A_Config { public function __construct(Array $test) ... } I want to be able to pass in…
Supericy
  • 5,866
  • 1
  • 21
  • 25
17
votes
4 answers

How to check for arrayness in PHP?

The best I could come up is function is_array_alike($array) { return is_array($array) || (is_object($array) && $array instanceof ArrayAccess && $array instanceof Traversable && $array instanceof Serializable && $array instanceof…
chx
  • 11,270
  • 7
  • 55
  • 129
16
votes
1 answer

Arrayaccess and native php array functions

Is there any way to use array_merge(), array_pop(), .. functions to work with ArrayAccess? Since now i've tried Iterate interface and __set_state() magic method with no success. Error that is given: array_replace_recursive() [
Kristian
  • 3,283
  • 3
  • 28
  • 52
16
votes
2 answers

ArrayAccess in PHP -- assigning to offset by reference

First, a quote from the ole' manual on ArrayAccess::offsetSet(): This function is not called in assignments by reference and otherwise indirect changes to array dimensions overloaded with ArrayAccess (indirect in the sense they are made not by…
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
13
votes
6 answers

inheritance of ArrayAccess: Uncaught ErrorException: Collection::offsetExists($key)

NB: Local server PHP Version 8.1.4, laravel project inside composer.json file have "php": "^7.2.5", version & "laravel/framework": "^7.0" PHP Fatal error: During inheritance of ArrayAccess: Uncaught ErrorException: Return type of…
Alal Sardar
  • 139
  • 1
  • 1
  • 4
12
votes
1 answer

Is there subscript syntax to extract a diagonal from a 2D Array?

I mostly can follow the syntax to 'drill down/slice' into an array with multiple dimensions (and flattening) on the docs page. A very cool feature. For example given: my @a=[[1,2,3], [4,5,6], [7,8,9]]; I can select column 2 of the…
drclaw
  • 2,463
  • 9
  • 23
10
votes
3 answers

PHP 5.4's simplified string offset reading

As many of you already know, PHP 5.4 alpha has been released. I have a question regarding the following. Simplified string offset reading. $str[1][0] is now a legal construct. How exactly does $str[1][0] work? EDIT:…
webjawns.com
  • 2,300
  • 2
  • 14
  • 34
8
votes
2 answers

Mocking/Stubbing an Object of a class that implements arrayaccess in PHPUnit

Here is the constructor of the class I am writing a test suite for (it extends mysqli): function __construct(Config $c) { // store config file $this->config = $c; // do mysqli constructor parent::__construct( …
AndyPerlitch
  • 4,539
  • 5
  • 28
  • 43
6
votes
3 answers

array_values doesn't work with ArrayAccess object

array_values() doesn't work with ArrayAccess object. neither does array_keys() why? if I can access $object['key'] I should be able to do all kind of array operations
thelolcat
  • 10,995
  • 21
  • 60
  • 102
6
votes
1 answer

Using SplObjectStorage as a data map, can you use a mutable array as the data?

In the following code: $storage = new \SplObjectStorage(); $fooA = new \StdClass(); $fooB = new \StdClass(); $storage[$fooA] = 1; $storage[$fooB] = array(); $storage[$fooA] = 2; $storage[$fooB][] = 'test'; I would expect $storage[$fooA] to be 1,…
Joe Lencioni
  • 10,231
  • 18
  • 55
  • 66
4
votes
1 answer

php 8.1 - return type deprecated in older script

Trying to update to php 8.1 and noticed this deprecated notice showing up in the error logs I'd like to take care of. [14-Feb-2022 14:48:25 UTC] PHP Deprecated: Return type of TLDExtractResult::offsetExists($offset) should either be compatible with…
user756659
  • 3,372
  • 13
  • 55
  • 110
4
votes
1 answer

PHP 5.6: ArrayAccess: Function isset calls offsetGet and causes undefined index notice

I wrote simple PHP class that implements ArrayAccess Interface: class MyArray implements ArrayAccess { public $value; public function __construct($value = null) { $this->value = $value; } public function…
Filip Š
  • 746
  • 2
  • 13
  • 22
4
votes
3 answers

php Converting an object implementing ArrayAccess in array

When you implement the _toString method on a class, you are able to convert the object in string $string =(string) $object Is there an equivalent for converting in array $array=(array) $object From what I have tested, with this code, the…
Arnaud
  • 41
  • 1
  • 2
1
2 3 4