State::offsetSet()

Sets a state to the specified key.

Table of Contents
  1. Description
  2. Example

Description

State::offsetSet(mixed $key, mixed $value): void;

This method is a requirement to be able to implement the ArrayAccess interface and aims to make the object data modifiable using array access syntax. You won’t use this method directly, it will be executed automatically when you try to set a state with array access syntax. This method sets a state to the specified key. If the key already exists, this method will override the state with that specific key.

Example

$state = new State(['foo' => 1]);

$state['bar'] = '#';
$state['foo'] = '#';
$state[] = '#';
$state[] = '#';

test($state->get([], true)); // Returns `['bar' => '#', 'foo' => '#', 0 => '#', 1 => '#']`

State::offsetSet()

Sets a state to the specified key.