Skip to content

StorageArray

Brownie edited this page Oct 25, 2017 · 3 revisions

An example of a storage description when a list of fields is not known in advance.

class StorageArrayExample extends StorageArray
{
}

Initializes the storage to supported fields.

$storageArrayExample = new StorageArrayExample(
    array(
        'id' => 10,
        'isExample' => true,
        'dateTime' => '2017-01-01 00:00:00',
        'reason' => 'Test'
    ),
    true
);

An example of a storage description with pre-known fields.

class StorageArrayExample extends StorageArray
{
    /**
     * List of supported fields.
     *
     * @var array
     */
    protected $fields = array(
        'id' => null,
        'isExample' => null,
        'dateTime' => '2012-03-03 03:03:03',
        'reason' => null
    );
}

Initializes the storage to supported fields.

$storageArrayExample = new StorageArrayExample(
    array(
        'id' => 20,
        'isExample' => false,
        'dateTime' => '2017-02-02 23:59:59',
        'reason' => 'Example'
    )
);

Examples of using.

/**
 * Sets the value in the store for supported field.
 */
$storageArrayExample->setReason('Hello');
$storageArrayExample->setIsExample(true);
/**
 * Gets a value from the store for supported field.
 */
$reason = $storageArrayExample->getReason();
$isExample = $storageArrayExample->getIsExample();
/**
 * Gets the storage as an associative array.
 */
$storageToArray = $storageArrayExample->toArray();

An exception "UndefinedMethodException" is thrown when accessing an unsupported field.

Clone this wiki locally