Skip to content

Instantly share code, notes, and snippets.

@ziggi
Last active December 23, 2015 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziggi/6652834 to your computer and use it in GitHub Desktop.
Save ziggi/6652834 to your computer and use it in GitHub Desktop.
Usage arrays with OOP
<?php
inculde 'oop_config.php';
echo Config::get()->site->path;
// result: some_path/
$cfg = new Config;
echo $cfg->site->path;
// result: some_path/
<?php
class Config extends App
{
private $_pointer = null;
private $_cfg = array(
'debug' => true,
'site' => array(
'path' => 'some_path/',
),
);
static function get() {
return (new Config);
}
function __construct() {
}
function __get($name)
{
if ($this->_pointer === null) {
if (isset($this->_cfg[$name])) {
$this->_pointer = $this->_cfg[$name];
} else {
return null;
}
} else {
if (isset($this->_pointer[$name])) {
$this->_pointer = $this->_pointer[$name];
} else {
return false;
}
}
if (is_array($this->_pointer)) {
return $this;
} else {
$result = $this->_pointer;
$this->_pointer = null;
return $result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment