Skip to content

Instantly share code, notes, and snippets.

@seebz
Created October 7, 2011 15:52
Show Gist options
  • Save seebz/1270623 to your computer and use it in GitHub Desktop.
Save seebz/1270623 to your computer and use it in GitHub Desktop.
BasicArrayObject
<pre><?php
error_reporting(-1);
class BasicArrayObject implements ArrayAccess {
protected $_vars = array();
public function __construct(array $vars = array()) {
foreach($vars as $key => & $value) {
$this->{$key} = $value;
}
}
function __clone() {
foreach ($this as $key => $val) {
if (is_object($val) || (is_array($val))) {
$this->{$key} = unserialize(serialize($val));
}
}
}
public function & __get($key) {
$getter = 'get_' . $key;
if ( method_exists($this, $getter) ) {
$var = $this->$getter($key); return $var;
} elseif ( isset($this->_vars[ $key ]) ) {
$var = & $this->_vars[ $key ]; return $var;
}
$this->_trigger_error(
sprintf('Undefined property: %s::$%s', get_class($this), $key),
E_USER_NOTICE
);
$var = null; return $var;
}
public function __set($key, $value) {
$setter = 'set_' . $key;
if ( method_exists($this, $setter) ) {
return $this->$setter($value);
} else {
return $this->_vars[ $key ] = $value;
}
}
public function __isset($key) {
return isset($this->_vars[ $key ]);
}
public function __unset($key) {
unset($this->_vars[ $key ]);
}
public function & offsetGet($key) {
// You can return by reference in offsetGet as of PHP 5.3.4
$var = & $this->__get($key); return $var;
}
public function offsetSet($key, $value) {
return $this->__set($key, $value);
}
public function offsetExists($key) {
return $this->__isset($key);
}
public function offsetUnset($key) {
return $this->__unset($key);
}
protected function _trigger_error($msg, $type = E_USER_NOTICE, $file = null, $line = null) {
if ( ($type === E_USER_ERROR && !(error_reporting() & E_ERROR))
|| ($type === E_USER_WARNING && !(error_reporting() & E_WARNING))
|| ($type === E_USER_NOTICE && !(error_reporting() & E_NOTICE))
|| ($type === E_USER_DEPRECATED && !(error_reporting() & E_WARNING)) ) {
return;
}
$debug = debug_backtrace();
$callee = next($debug);
if (is_null($file)) {
$file = $callee['file'];
}
if (is_null($line)) {
$line = $callee['line'];
}
$msg .= sprintf(' in %s on line %d', $file, $line);
set_error_handler(array($this, '_error_handler'), $type);
trigger_error($msg, $type);
restore_error_handler();
}
protected function _error_handler($errno, $errstr) {
switch($errno) {
case E_USER_ERROR: $errtype = 'Error'; break;
case E_USER_WARNING: $errtype = 'Warning'; break;
case E_USER_NOTICE: $errtype = 'Notice'; break;
case E_USER_DEPRECATED: $errtype = 'Deprecated'; break;
default: $errtype = 'Unknown';
}
if (ini_get('display_errors')) {
printf("<br />\n<b>%s</b>: %s<br /><br />\n", $errtype, $errstr);
}
if (ini_get('log_errors')) {
error_log(sprintf('PHP %s: %s', $errtype, $errstr));
}
return true;
}
}
$y = new StdClass();
$y->inexistant;
$x = new BasicArrayObject( array('test' => 'TEST') );
$x->inexistant;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment