Trait Yiisoft\Arrays\ArrayAccessTrait
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.
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $data | array | Yiisoft\Arrays\ArrayAccessTrait |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| count() | Returns the number of data items. | Yiisoft\Arrays\ArrayAccessTrait |
| getIterator() | Returns an iterator for traversing the data. | Yiisoft\Arrays\ArrayAccessTrait |
| offsetExists() | This method is required by the interface ArrayAccess. | Yiisoft\Arrays\ArrayAccessTrait |
| offsetGet() | This method is required by the interface ArrayAccess. | Yiisoft\Arrays\ArrayAccessTrait |
| offsetSet() | This method is required by the interface ArrayAccess. | Yiisoft\Arrays\ArrayAccessTrait |
| offsetUnset() | This method is required by the interface ArrayAccess. | Yiisoft\Arrays\ArrayAccessTrait |
Property Details
Method Details
Returns the number of data items.
This method is required by Countable interface.
| public count( ): integer | ||
| return | integer |
Number of data elements. |
|---|---|---|
public function count(): int
{
return count($this->data);
}
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.
| public getIterator( ): ArrayIterator | ||
| return | ArrayIterator |
An iterator for traversing the cookies in the collection. |
|---|---|---|
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->data);
}
This method is required by the interface ArrayAccess.
| public offsetExists( mixed $offset ): boolean | ||
| $offset | mixed |
The offset to check on. |
public function offsetExists(mixed $offset): bool
{
return isset($this->data[$offset]);
}
This method is required by the interface ArrayAccess.
| public offsetGet( mixed $offset ): mixed | ||
| $offset | mixed |
The offset to retrieve element. |
| return | mixed |
The element at the offset, null if no element is found at the offset. |
|---|---|---|
public function offsetGet(mixed $offset): mixed
{
return $this->data[$offset] ?? null;
}
This method is required by the interface ArrayAccess.
| public offsetSet( mixed $offset, mixed $value ): void | ||
| $offset | mixed |
The offset to set element. |
| $value | mixed |
The element value. |
public function offsetSet(mixed $offset, mixed $value): void
{
if ($offset === null) {
$this->data[] = $value;
} else {
$this->data[$offset] = $value;
}
}
This method is required by the interface ArrayAccess.
| public offsetUnset( mixed $offset ): void | ||
| $offset | mixed |
The offset to unset element. |
public function offsetUnset(mixed $offset): void
{
unset($this->data[$offset]);
}
Signup or Login in order to comment.