On gists
PHP DataContainer class
PHP
data-container.php
Raw
#
<?php
class dataContainer_helper implements ArrayAccess {
protected $data;
public function __toString() {
return '';
}
// array access
public function offsetSet($offset, $value) {
$this->__set($offset, $value);
}
public function offsetUnset($offset) {
$this->__unset($offset);
}
public function offsetExists($offset) {
return $this->__isset($offset);
}
public function offsetGet($offset) {
//return $this->{$offset};
return $this->__get($offset);
}
public function __get($name) {
if(!isset($this->data[$name])) {
$this->data[$name] = new dataContainer_helper;
}
//var_dump($this->data[$name]);
return $this->data[$name];
}
public function __set($name, $value) {
if(is_array($value) && !is_int(key($value))) {
if(!($this->data[$name] instanceof dataContainer_helper)) {
$this->data[$name] = new dataContainer_helper;
}
foreach($value as $key => $v) {
$this->data[$name]->$key = $v;
}
} else {
$this->data[$name] = $value;
}
}
public function __unset($name) {
unset($this->data[$name]);
}
public function __isset($name) {
if(isset($this->data[$name])) {
if($this->data[$name] instanceof dataContainer_helper) {
if($this->data[$name]->isNull()) {
//var_dump($this->data[$name]);
return false;
}
}
if($this->data[$name] !== null) {
return true;
}
}
return false;
}
public function getContainerData() {
if(is_array($this->data)) {
$data = array();
foreach($this->data as $key => $value) {
if($value instanceof dataContainer_helper) {
$data[$key] = $value->getContainerData();
if($data[$key] === false) {
unset($data[$key]);
}
} else {
$data[$key] = $value;
}
}
return $data;
}
return false;
}
public function setContainerData($data) {
if(is_array($data)) {
foreach($data as $key => $value) {
$this->$key = $value;
}
} else {
return false;
}
}
public function isNull() {
if($this->data === null) {
return true;
} else {
return false;
}
}
}
$a = new dataContainer_helper;
echo $a->kkt->b = 'ku';