[ create a new paste ] login | about

Link: http://codepad.org/9EZ2Hqf8    [ raw code | output | fork ]

PHP, pasted on May 6:
<?php
// set debug
error_reporting(E_ALL ^ E_STRICT);
ini_set('display_errors', 'on');

class Config
{
	protected $data;
	
	public function __construct($data = null) {
		$this->data = $data;
	}
	
	public function __get($name) {
		return isset($this->data[$name]) ? new Config($this->data[$name]) : new Config();
	}
	
	public function isEmpty() {
		return empty($this->data);
	}
	
	public function get() {
		return $this->data;
	}
}

$test = new Config(array(
	'foo' => array(
		'bar' => array(
			'barfoo' => 1
		)
	)
));


// test 1
if (!$test->foo->bar->isEmpty()) {
	print_r($test->foo->bar->get());
}

// test 2
if (!$test->foo->bar->foobar->isEmpty()) {
	print_r($test->foo->bar->foobar->get());
}

// test 3
if (!$test->foo->bar->barfoo->isEmpty()) {
	print_r($test->foo->bar->barfoo->get());
}


Output:
1
2
3
4
5
Array
(
    [barfoo] => 1
)
1


Create a new paste based on this one


Comments: