State::offsetGet()

Gets a state using its key.

Table of Contents
  1. Description
  2. Example

Description

State::offsetGet(mixed $key): mixed;

This method is a requirement to be able to implement the ArrayAccess interface and aims to make the object data accessible using array access syntax. You won’t use this method directly, it will be executed automatically when you try to get a state with array access syntax. This method gets a state using its key. If the key does not exist, this method returns null.

Example

$state = new State([
    'bar' => 1,
    'baz-qux' => 2,
    'foo' => ""
]);

test($state['asdf']); // Returns `null`
test($state['bar']); // Returns `1`
test($state['baz-qux']); // Returns `2`
test($state['bazQux']); // Returns `null`
test($state->bazQux); // Returns `2` (see `State::__get()`)
test($state->{'baz-qux'}); // Returns `2` (see `State::__get()`)
test($state['foo']); // Returns `""`

State::offsetGet()

Gets a state using its key.