Skip to content

Instantly share code, notes, and snippets.

@hejrobin
Created May 2, 2011 13:10
Show Gist options
  • Save hejrobin/951590 to your computer and use it in GitHub Desktop.
Save hejrobin/951590 to your computer and use it in GitHub Desktop.
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
class InternalStore {
protected $store;
public function __construct($store = array()) {
$this->setStore($store);
}
public function setStore($store) {
if(is_array($store))
$this->store = $store;
}
public function getStore() {
return $this->store;
}
public function __set($key, $item) {
$this->store[$key] = $item;
}
public function __get($key) {
if(!array_key_exists($key, $this->store))
$this->store[$key] = array();
if(array_key_exists($key, $this->store)) {
if(is_array($this->store[$key]))
return new InternalStoreObject($key, $this->store[$key], $this);
else return $this->store[$key];
}
return (object) array();
}
public function __toString() {
return __CLASS__;
}
}
class InternalStoreObject {
protected $store;
protected $key;
protected $parent;
public function __construct($key, $store, $parent) {
$this->key = $key;
$this->store = $store;
$this->parent = $parent;
}
public function __set($key, $item) {
$this->store[$key] = $item;
$key = $this->key;
$this->parent->$key = $this->store;
}
public function __get($key) {
if(!array_key_exists($key, $this->store))
$this->store[$key] = array();
if(array_key_exists($key, $this->store)) {
if(is_array($this->store[$key]))
return new InternalStoreObject($key, $this->store[$key], $this);
else return $this->store[$key];
}
return (object) array();
}
public function __toString() {
return $this->key;
}
}
$store = new InternalStore(array(
'what' => array(
'is' => array(
'love' => "Baby, don't hurt me&hellip;",
'www' => 'A series of tubes!'
)
),
'rick' => array(
'roll' => "Never gonna give you up&hellip;",
'troll' => 'RickarN? ;)'
)
));
$store->rick->troll = 'U MAD BRAH?';
$store->what->ovr9k = "UNBERIVABURR! ( ~ __ ~ )";
$store->foo->bar->baz->lol->face->dot->jpeg = ':D';
echo '<pre>';
print_r($store->getStore());
echo '</pre>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment