# Stores

Stores are objects that implement READ/UPDATE/DELETE operations against a storage medium for objects that implement the [StorableInterface](https://github.com/deep-web-solutions/wordpress-framework-foundations/blob/master/src/includes/Utilities/Storage/StorableInterface.php) interface. In a nutshell, an object is storable if it has an ID (see the [StorableTrait ](https://github.com/deep-web-solutions/wordpress-framework-foundations/blob/master/src/includes/Utilities/Storage/StorableTrait.php)and [AbstractStorable](https://github.com/deep-web-solutions/wordpress-framework-foundations/blob/master/src/includes/Utilities/Storage/AbstractStorable.php) for a simple implementation). All classes belonging to the Storage namespace can be found [here](https://github.com/deep-web-solutions/wordpress-framework-foundations/tree/master/src/includes/Utilities/Storage).

There are 3 stores that the Foundations Module comes with:

* [A memory store](https://github.com/deep-web-solutions/wordpress-framework-foundations/blob/master/src/includes/Utilities/Storage/Stores/MemoryStore.php) for storing objects in-memory during the current request.
* [An options table store](https://github.com/deep-web-solutions/wordpress-framework-foundations/blob/master/src/includes/Utilities/Storage/Stores/OptionsStore.php) for persistent database storage across requests in the WP options table.
* [A user meta table store](https://github.com/deep-web-solutions/wordpress-framework-foundations/blob/master/src/includes/Utilities/Storage/Stores/UserMetaStore.php) for persistent database storage across request in the WP user meta table.

{% hint style="info" %}
Stores are storable as well. It is possible to create a store that stores other stores! Check the [`MultiStoreAwareInterface`](https://github.com/deep-web-solutions/wordpress-framework-foundations/blob/master/src/includes/Utilities/Storage/MultiStoreAwareInterface.php) and [`MultiStoreAwareTrait`](https://github.com/deep-web-solutions/wordpress-framework-foundations/blob/master/src/includes/Utilities/Storage/MultiStoreAwareTrait.php) for examples.
{% endhint %}

Here is a simple example of a dummy storable object stored in a memory store:

```php
<?php

namespace DeepWebSolutions\Plugins\MyTestPlugin\Storage;

use DeepWebSolutions\Framework\Foundations\Utilities\Storage\AbstractStorable;
use DeepWebSolutions\Framework\Foundations\Utilities\Storage\Stores\MemoryStore;

class MyStorable extends AbstractStorable {
    public string $storable_property;
}

$my_storable1 = new MyStorable( 'unique-id-1' );
$my_storable1->storable_property = 'test value 1';

$my_storable2 = new MyStorable( 'unique-id-2' );
$my_storable1->storable_property = 'test value 2';

$memory_store = new MemoryStore( 'my-memory-store' );
$memory_store->add( $my_storable1 );
$memory_store->add( $my_storable2 );

echo $memory_store->count(); // echoes 2
echo $memory_store->get( 'unique-id-1' )->storable_property; // echoes 'test value 1'

$my_storable3 = new MyStorable( 'unique-id-1' ); // same as $my_storable1
$my_storable3->storable_property = 'test value 3';
$memory_store->update( $my_storable3 );

echo $memory_store->get( 'unique-id-1' )->storable_property; // echoes 'test value 3'

```

{% hint style="danger" %}
There is no guarantee that your storable object will be stored properly when using the options or user meta stores. The two stores rely on WordPress' own [`update_option`](https://developer.wordpress.org/reference/functions/update_option/) and [`update_user_meta`](https://developer.wordpress.org/reference/functions/update_user_meta/) functions respectively and those both rely on PHP's [`serialize`](https://www.php.net/manual/en/function.serialize.php) function. If you have trouble storing your objects, implement the magic methods `__serialize` and `__unserialize` for better handling.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://framework.deep-web-solutions.com/foundations-module/utilities/stores.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
