Last active
December 23, 2015 14:59
-
-
Save ziggi/6652834 to your computer and use it in GitHub Desktop.
Usage arrays with OOP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
inculde 'oop_config.php'; | |
echo Config::get()->site->path; | |
// result: some_path/ | |
$cfg = new Config; | |
echo $cfg->site->path; | |
// result: some_path/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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