Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple PHP Key-Value DataStore
<?php
namespace Bramus;
/**
* Simple Key-Value DataStore to keep variables on
*/
class Datastore {
private $data = array();
public function __get($name) {
return isset($this->data[$name]) ? $this->data[$name] : null;
}
public function __set($name, $value) {
$this->data[$name] = $value;
}
public function __call($name, $args) {
return isset($this->data[$name]) ? $this->data[$name] : (isset($args[0]) ? $args[0] : null);
}
}
// EOF
@bramus

This comment has been minimized.

Copy link
Owner Author

@bramus bramus commented Apr 6, 2016

Usage

<?php

// Create instance
$ds = new \Bramus\Datastore();

// Setting a value
$ds->firstName = 'Bramus';

// Getting a value
echo $ds->firstName; // "Bramus"

// Getting a value with default fallback
echo $ds->lastName; // null
echo $ds->lastName("Van Damme"); // "Van Damme"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment