ArrayAccessTrait
API reference › yii › base › ArrayAccessTrait
Used by 2 types
ArrayAccessTrait provides the implementation for IteratorAggregate, ArrayAccess and Countable.
Note that ArrayAccessTrait requires the class using it contain a property named data which should be an array.
The data will be exposed by ArrayAccessTrait to support accessing the class object like an array.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$data | array<array-key, | ArrayAccessTrait |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
count() | Returns the number of data items. | ArrayAccessTrait |
getIterator() | Returns an iterator for traversing the data. | ArrayAccessTrait |
offsetExists() | This method is required by the interface ArrayAccess. | ArrayAccessTrait |
offsetGet() | This method is required by the interface ArrayAccess. | ArrayAccessTrait |
offsetSet() | This method is required by the interface ArrayAccess. | ArrayAccessTrait |
offsetUnset() | This method is required by the interface ArrayAccess. | ArrayAccessTrait |
Method details
Section titled “Method details”count()
Section titled “count() ”public function count(): intReturns the number of data items.
This method is required by Countable interface.
Returns
int: Number of data elements.
getIterator()
Section titled “getIterator() ”public function getIterator(): ArrayIterator<array-key, mixed>Returns an iterator for traversing the data.
This method is required by the SPL interface IteratorAggregate.
It will be implicitly called when you use foreach to traverse the collection.
Returns
ArrayIterator<array-key, mixed>: An iterator for traversing the cookies in the collection.
offsetExists()
Section titled “offsetExists() ”public function offsetExists(int|string $offset): boolThis method is required by the interface ArrayAccess.
Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | int| | The offset to check on |
Returns
bool
offsetGet()
Section titled “offsetGet() ”public function offsetGet(int|string $offset): mixedThis method is required by the interface ArrayAccess.
Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | int| | The offset to retrieve element. |
Returns
mixed: The element at the offset, null if no element is found at the offset
offsetSet()
Section titled “offsetSet() ”public function offsetSet(int|string $offset, mixed $item): mixedThis method is required by the interface ArrayAccess.
Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | int| | The offset to set element |
$item | mixed | The element value |
Returns
mixed
offsetUnset()
Section titled “offsetUnset() ”public function offsetUnset(int|string $offset): mixedThis method is required by the interface ArrayAccess.
Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | int| | The offset to unset element |
Returns
mixed