Skip to content
PHP Ecosystem
Explore

ArrayAccessTrait

API reference › yii › base › ArrayAccessTrait

traityii\base\ArrayAccessTrait
Since2.0
Sourceframework/base/ArrayAccessTrait.php
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.

PropertyTypeDescriptionDefined by
$dataarray<array-key, mixed>ArrayAccessTrait
MethodDescriptionDefined 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
public function count(): int

Returns the number of data items.

This method is required by Countable interface.

Returns

int: Number of data elements.

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.

public function offsetExists(int|string $offset): bool

This method is required by the interface ArrayAccess.

Parameters

ParameterTypeDescription
$offsetint|stringThe offset to check on

Returns

bool

public function offsetGet(int|string $offset): mixed

This method is required by the interface ArrayAccess.

Parameters

ParameterTypeDescription
$offsetint|stringThe offset to retrieve element.

Returns

mixed: The element at the offset, null if no element is found at the offset

public function offsetSet(int|string $offset, mixed $item): mixed

This method is required by the interface ArrayAccess.

Parameters

ParameterTypeDescription
$offsetint|stringThe offset to set element
$itemmixedThe element value

Returns

mixed

public function offsetUnset(int|string $offset): mixed

This method is required by the interface ArrayAccess.

Parameters

ParameterTypeDescription
$offsetint|stringThe offset to unset element

Returns

mixed