我如何解析ini文件作为对象

时间:2015-01-29 08:35:22

标签: php ini

我按如下方式获取ini文件的内容

$file = 'config.ini';
if (!file_exists($file)) {
    $file = 'config.sample.ini';
}
$config = parse_ini_file($file, true);

这导致了一个多维数组,但我认为它是一个对象。这可能与parse_ini_file()有关吗?我将如何解决这个问题?

4 个答案:

答案 0 :(得分:5)

您可以使用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);

答案 1 :(得分:0)

如果要将数组转换为stdClass,只需使用:

$configObj = (object) parse_ini_file($file, true);

答案 2 :(得分:0)

  • 您可以使用两种方法将数组更改为对象。


    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"
    }

答案 3 :(得分:0)

如果你有一些推进机制尝试下面的代码示例。

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'));