PHPackages                             meyersm/simple-datastore - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. meyersm/simple-datastore

ActiveLibrary[Database &amp; ORM](/categories/database)

meyersm/simple-datastore
========================

A PHP library to store objects between sessions using only the local file system

0.5.2(12y ago)121PHPPHP &gt;=5.3.0

Since Jan 18Pushed 12y ago1 watchersCompare

[ Source](https://github.com/meyersm/simpleDatastore)[ Packagist](https://packagist.org/packages/meyersm/simple-datastore)[ Docs](https://github.com/meyersm/simpleDatastore)[ RSS](/packages/meyersm-simple-datastore/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (1)DependenciesVersions (4)Used By (0)

simpleDatastore
===============

[](#simpledatastore)

[![Build Status](https://camo.githubusercontent.com/c41b70effc8f2af5a0286d2b8da4ed1a104603a7bca322ce93f6bf2b5703f303/68747470733a2f2f7472617669732d63692e6f72672f6d65796572736d2f73696d706c654461746173746f72652e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/meyersm/simpleDatastore)

A PHP library to store objects between sessions using only the local file system.

Objects can be stored in json or serialized formats and be opened in read only mode or a read/write mode that locks the datastore to prevent overwriting or race conditions.

Good uses for simpleDatastore

- Need to preserve data between scripts/sessions and a database is overkill or not feasable
- Need to share data with another non-php script/code and json is the most convienient
- Fallback to preserve data incase a database connection failed.

This library should NOT be used as a replacement for a database in any application that has high traffic and many concurrent writes, while the files are locked while writing, every other thread/script that wants to write will have to wait for the lock causing slowdowns.

Installation
------------

[](#installation)

To install with composer, add this to your composer.json

```
	"require": {
        "meyersm/simple-datastore":">=0.5.2"
	},

```

Then run `composer update` and add this to your code,

```
use meyersm\simpleDatastore;
```

Alternativly you can just download the simpleDatastore.php file and add this to your code,

```
require('simpleDatastore.php');
use meyersm\simpleDatastore;
```

Usage
-----

[](#usage)

Creating a new Datastore:

```
$datastore = new simpleDatastore("datastore-name");
```

Then you can start adding data, and save to the new datastore

```
$datastore->fish_hooks = 50;
$datastore->fish_names = array("bob","archibold","thomas");
$datastore->enable_fish= true;
$datastore['array_access'] = "yup";
$datastore[] = "new array entry";
$datastore->save();
```

The datastore still has a write lock on the file until the file is closed, at that point another script can open and read the data

```
$datastore->close();
//You can also call $datastore->save(true) to save then close in 1 line
$NEWdatastore = new simpleDatastore("datastore-name");
print $NEWdatastore->fish_hooks; //50
print $NEWdatastore['fish_names'][0] //bob
```

If you want to delete a datastore file, use the destroy function

```
$datastore->destroy();
```

When you try to open a datastore that already has a lock on it, the library will re-try for a configurable amount of time then error out. If you want to access a datastore while its locked, you can open it in read only mode

```
$datastore = new simpleDatastore("myDatastore",true);
```

Working with php classes that would be better stored as serialized objects instead of json? Set the serialize flag to true when opening a datastore

```
$datastore = new simpleDatastore("myDatastore",false,true);
$datastore->complexClass = $CC;
$datastore->otherDatastore = $someOtherDatastore //You can even serialize other simpleDatastore objects if you like.
```

By default the library will store all datastores and thier lock files in a sub directory called "datastore". It also throws exceptions in case of an error, if you would like to change either of those defaults you can.

```
$datastore = new simpleDatastore("teststore",false,false,"anotherDirectory"); //Different directory for datastore files
$teamB = new simpleDatastore(); //If you are going to change the error mode, you may want to wait to load the file instead of doing so at instantiation

$teamB->error_mode = simpleDatastore::$ERROR_MODE_SILENT; //Fails silently, will leave datastore null if error on read
$teamB->error_mode = simpleDatastore::$ERROR_MODE_DIE; //Dies on fatal error, ending script execution
$teamB->error_mode = simpleDatastore::$ERROR_MODE_EXCEPTION; //Default, throws exceptions on fatal errors
$teamB->open("teststore");
```

To change the way the library handles a locked datastore file, use the setLockConfig function. Again if doing this, delay opening the datastore file like in the example above

```
/**
* @param int $secondsBetweenLockAttempts Seconds to wait between lock attempts
* @param int $lockAttempts Number of tries to try and lock datastore
*/
public function setLockConfig($secondsBetweenLockAttempts=1,$lockAttempts=20)
```

For more examples on how to use this library, check out the unit tests

[Unit tests](tests/simpleDatastoreTest.php)

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

3

Last Release

4544d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/444573?v=4)[Michael Meyers](/maintainers/meyersm)[@meyersm](https://github.com/meyersm)

---

Top Contributors

[![meyersm](https://avatars.githubusercontent.com/u/444573?v=4)](https://github.com/meyersm "meyersm (15 commits)")

---

Tags

databasedatastore

### Embed Badge

![Health badge](/badges/meyersm-simple-datastore/health.svg)

```
[![Health](https://phpackages.com/badges/meyersm-simple-datastore/health.svg)](https://phpackages.com/packages/meyersm-simple-datastore)
```

###  Alternatives

[doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.

9.7k595.8M6.5k](/packages/doctrine-dbal)[doctrine/orm

Object-Relational-Mapper for PHP

10.2k295.3M7.2k](/packages/doctrine-orm)[doctrine/doctrine-bundle

Symfony DoctrineBundle

4.8k249.9M3.9k](/packages/doctrine-doctrine-bundle)[doctrine/migrations

PHP Doctrine Migrations project offer additional functionality on top of the database abstraction layer (DBAL) for versioning your database schema and easily deploying changes to it. It is a very easy to use and a powerful tool.

4.8k212.9M509](/packages/doctrine-migrations)[doctrine/data-fixtures

Data Fixtures for all Doctrine Object Managers

2.9k141.0M566](/packages/doctrine-data-fixtures)[robmorgan/phinx

Phinx makes it ridiculously easy to manage the database migrations for your PHP app.

4.6k48.7M443](/packages/robmorgan-phinx)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
