Skip to content

Instantly share code, notes, and snippets.

@seebz
Created October 20, 2014 13:38
Show Gist options
  • Select an option

  • Save seebz/7f4aa0634d9e032ae8e9 to your computer and use it in GitHub Desktop.

Select an option

Save seebz/7f4aa0634d9e032ae8e9 to your computer and use it in GitHub Desktop.
<?php
class SuperObject implements ArrayAccess, Countable
{
/**
* @param array
*/
public function __construct(array $data = array())
{
foreach ($data as $k => $v) {
$this->{$k} = $v;
}
}
/**
* @param string $name
* @param array $arguments
*/
public function __call($name, array $arguments)
{
if (isset($this->{$name}) && is_callable($this->{$name})) {
return call_user_func_array($this->{$name}, $arguments);
}
else {
$debug = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$d = (object) next($debug);
trigger_error("Call to undefined method {$d->class}::{$d->function}() in {$d->file} on line {$d->line} -- triggered", E_USER_ERROR);
}
}
/**
* @param scalar $name
* @return mixed
*/
public function __get($name)
{
$debug = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$d = (object) current($debug);
trigger_error("Undefined property: {$d->class}::{$name} in {$d->file} on line {$d->line} -- triggered", E_USER_NOTICE);
}
/**
* @param scalar $offset
* @param mixed $value
* @return void
*/
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
static $index = 0;
$this->{$index} = $value;
$index++;
}
else {
$this->{$offset} = $value;
}
}
/**
* @param scalar $offset
* @return boolean
*/
public function offsetExists($offset)
{
return isset($this->{$offset});
}
/**
* @param scalar $offset
* @return void
*/
public function offsetUnset($offset)
{
unset($this->{$offset});
}
/**
* @param scalar $offset
* @return mixed
*/
public function offsetGet($offset)
{
if (isset($this->{$offset})) {
return $this->{$offset};
}
else {
$debug = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$d = (object) current($debug);
trigger_error("Undefined index: {$offset} in {$d->file} on line {$d->line} -- triggered", E_USER_NOTICE);
}
}
/**
* @return integer
*/
public function count()
{
return count(get_object_vars($this));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment