Skip to content

Instantly share code, notes, and snippets.

@bramus
Created April 6, 2016 15:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bramus/5fafea6bbe16415a9cef11ff51530900 to your computer and use it in GitHub Desktop.
Save bramus/5fafea6bbe16415a9cef11ff51530900 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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