如何将 ini 文件解析为对象
How can i parse an ini file as object
我正在如下获取 ini 文件的内容
$file = 'config.ini';
if (!file_exists($file)) {
$file = 'config.sample.ini';
}
$config = parse_ini_file($file, true);
这会产生一个多维数组,但我更希望它是一个对象。 parse_ini_file() 这可能吗?我该怎么做?
如果你想将数组转换为 stdClass,只需使用这个:
$configObj = (object) parse_ini_file($file, true);
您可以使用 json_encode()
和 json_decode()
来实现:
$file = 'config.ini';
if (!file_exists($file)) {
$file = 'config.sample.ini';
}
$config = parse_ini_file($file, true);
// convert to data to a json string
$config = json_encode($config);
// convert back from json, the second parameter is by
// default false, which will return an object rather than an
// associative array
$config = json_decode($config);
- 您可以使用其他两种方法将数组更改为对象。
function array_to_object($array, &$object)
{
foreach ($array as $key => $value) {
if (is_array($value)) {
$object->$key = new stdClass();
array_to_object($value, $object->$key);
} else {
$object->$key = $value;
}
}
return $object;
}
function arrayToObject($array)
{
$object= new stdClass();
return array_to_object($array, $object);
}
- 例子
$configArr = array(
'NAME' => 'stack over flow',
'AGE' => 28,
'SEX' => 'MALE',
);
$configObj = arrayToObject($configArr);
var_dump($configObj);
- 输出
class stdClass#1 (3) {
public $NAME => string(15) "stack over flow"
public $AGE => int(28)
public $SEX => string(4) "MALE"
}
如果您对此有一些高级机制,请尝试以下代码示例。
INI 文件
[database]
databse.engine = mysqli
databse.host = localhost
databse.user = root
databse.password = demopass
databse.database = demodb
[cache]
enable_cache = false
class Config implements Countable, Iterator {
protected $_data;
protected $_count;
protected $_index;
public
function __construct($config = array()) {
if (sizeof($config) > 0) {
foreach($config as $key = > $value) {
if (is_array($value)) {
$this - > _data[$key] = new self($value);
} else {
$this - > _data[$key] = $value;
}
}
}
$this - > _count = count($this - > _data);
}
public
function get($name) {
return array_key_exists($name, $this - > _data) ? $this - > _data[$name] : null;
}
public
function __get($name) {
return $this - > get($name);
}
public
function __set($name, $value) {
if (is_array($value)) {
$this - > _data[$name] = new self($value);
} else {
$this - > _data[$name] = $value;
}
$this - > _count = count($this - > _data);
}
public
function __clone() {
$array = array();
foreach($this - > _data as $key = > $value) {
if ($value instanceof Config) {
$array[$key] = clone $value;
} else {
$array[$key] = $value;
}
}
$this - > _data = $array;
}
public
function toArray() {
$array = array();
$data = $this - > _data;
foreach($data as $key = > $value) {
if ($value instanceof Config) {
$array[$key] = $value - > toArray();
} else {
$array[$key] = $value;
}
}
return $array;
}
public
function __isset($name) {
return isset($this - > _data[$name]);
}
public
function __unset($name) {
unset($this - > _data[$name]);
$this - > _count = count($this - > _data);
}
public
function count() {
return $this - > _count;
}
function rewind() {
reset($this - > _data);
$this - > _index = 0;
}
function current() {
return current($this - > _data);
}
function key() {
return key($this - > _data);
}
function next() {
next($this - > _data);
$this - > _index++;
}
function valid() {
return $this - > _index < $this - > _count;
}
}
class parseINI extends Config {
protected $_filePath;
public
function __construct($iniPath = null) {
try {
if (!is_null($iniPath)) {
$this - > _filePath = $iniFile;
}
$this - > __validateIniFile();
$parse = parse_ini_file($this - > _filePath, true);
parent::__construct($parse);
} catch (ErrorException $ex) {
print $ex - > getMessage();
}
}
protected
function __validateIniFile() {
if (is_null($this - > _filePath)) {
throw new ErrorException('Please set the path of ini file.');
}
elseif(!strtolower(pathinfo($this - > _filePath, PATHINFO_EXTENSION)) === 'ini') {
throw new ErrorException('Provided file is not a valid ini file.');
}
elseif(!is_readable($this - > _filePath)) {
throw new ErrorException('Provided file location is not a valid.');
}
}
public
function getConfig() {
return new Config($this - > _data);
}
}
echo '<pre>';
$parse = new parseINI('data/php.ini');
print_r($parse - > getConfig());
print_r($parse - > getConfig() - > get('database'));
我正在如下获取 ini 文件的内容
$file = 'config.ini';
if (!file_exists($file)) {
$file = 'config.sample.ini';
}
$config = parse_ini_file($file, true);
这会产生一个多维数组,但我更希望它是一个对象。 parse_ini_file() 这可能吗?我该怎么做?
如果你想将数组转换为 stdClass,只需使用这个:
$configObj = (object) parse_ini_file($file, true);
您可以使用 json_encode()
和 json_decode()
来实现:
$file = 'config.ini';
if (!file_exists($file)) {
$file = 'config.sample.ini';
}
$config = parse_ini_file($file, true);
// convert to data to a json string
$config = json_encode($config);
// convert back from json, the second parameter is by
// default false, which will return an object rather than an
// associative array
$config = json_decode($config);
- 您可以使用其他两种方法将数组更改为对象。
function array_to_object($array, &$object)
{
foreach ($array as $key => $value) {
if (is_array($value)) {
$object->$key = new stdClass();
array_to_object($value, $object->$key);
} else {
$object->$key = $value;
}
}
return $object;
}
function arrayToObject($array)
{
$object= new stdClass();
return array_to_object($array, $object);
}
- 例子
$configArr = array(
'NAME' => 'stack over flow',
'AGE' => 28,
'SEX' => 'MALE',
);
$configObj = arrayToObject($configArr);
var_dump($configObj);
- 输出
class stdClass#1 (3) {
public $NAME => string(15) "stack over flow"
public $AGE => int(28)
public $SEX => string(4) "MALE"
}
如果您对此有一些高级机制,请尝试以下代码示例。
INI 文件
[database]
databse.engine = mysqli
databse.host = localhost
databse.user = root
databse.password = demopass
databse.database = demodb
[cache]
enable_cache = false
class Config implements Countable, Iterator {
protected $_data;
protected $_count;
protected $_index;
public
function __construct($config = array()) {
if (sizeof($config) > 0) {
foreach($config as $key = > $value) {
if (is_array($value)) {
$this - > _data[$key] = new self($value);
} else {
$this - > _data[$key] = $value;
}
}
}
$this - > _count = count($this - > _data);
}
public
function get($name) {
return array_key_exists($name, $this - > _data) ? $this - > _data[$name] : null;
}
public
function __get($name) {
return $this - > get($name);
}
public
function __set($name, $value) {
if (is_array($value)) {
$this - > _data[$name] = new self($value);
} else {
$this - > _data[$name] = $value;
}
$this - > _count = count($this - > _data);
}
public
function __clone() {
$array = array();
foreach($this - > _data as $key = > $value) {
if ($value instanceof Config) {
$array[$key] = clone $value;
} else {
$array[$key] = $value;
}
}
$this - > _data = $array;
}
public
function toArray() {
$array = array();
$data = $this - > _data;
foreach($data as $key = > $value) {
if ($value instanceof Config) {
$array[$key] = $value - > toArray();
} else {
$array[$key] = $value;
}
}
return $array;
}
public
function __isset($name) {
return isset($this - > _data[$name]);
}
public
function __unset($name) {
unset($this - > _data[$name]);
$this - > _count = count($this - > _data);
}
public
function count() {
return $this - > _count;
}
function rewind() {
reset($this - > _data);
$this - > _index = 0;
}
function current() {
return current($this - > _data);
}
function key() {
return key($this - > _data);
}
function next() {
next($this - > _data);
$this - > _index++;
}
function valid() {
return $this - > _index < $this - > _count;
}
}
class parseINI extends Config {
protected $_filePath;
public
function __construct($iniPath = null) {
try {
if (!is_null($iniPath)) {
$this - > _filePath = $iniFile;
}
$this - > __validateIniFile();
$parse = parse_ini_file($this - > _filePath, true);
parent::__construct($parse);
} catch (ErrorException $ex) {
print $ex - > getMessage();
}
}
protected
function __validateIniFile() {
if (is_null($this - > _filePath)) {
throw new ErrorException('Please set the path of ini file.');
}
elseif(!strtolower(pathinfo($this - > _filePath, PATHINFO_EXTENSION)) === 'ini') {
throw new ErrorException('Provided file is not a valid ini file.');
}
elseif(!is_readable($this - > _filePath)) {
throw new ErrorException('Provided file location is not a valid.');
}
}
public
function getConfig() {
return new Config($this - > _data);
}
}
echo '<pre>';
$parse = new parseINI('data/php.ini');
print_r($parse - > getConfig());
print_r($parse - > getConfig() - > get('database'));