开发者

Losing booleans when parsing *.ini-file using PHP

开发者 https://www.devze.com 2023-03-27 10:20 出处:网络
Just tried to make my own config handler in PHP, utilizing the native PHP parse_ini_file-function. I did just notice that all of my config entries where the value is a boolean - true/false are messed

Just tried to make my own config handler in PHP, utilizing the native PHP parse_ini_file-function. I did just notice that all of my config entries where the value is a boolean - true/false are messed up开发者_StackOverflow社区, along with most other config entries being typecasted to strings.

Apparently, this is a well-known issue already: http://www.php.net/manual/en/function.parse-ini-file.php#100851

So what is the appropriate workaround?

It's really neat to utilize non-PHP-configs, which makes it dead easy for non-developers to change a value when necessary, so I would love to keep the *.ini-based configs.


A little late to the party but as of PHP 5.6, you can pass the third argument INI_SCANNER_TYPED and it will maintain the type of value

parse_ini_file('config.ini', true, INI_SCANNER_TYPED)


I guess the most simple solution to the problem is to just use your own config handler. Either write a ini class or download a free one (ini format is extremely simple).

But I would recommend storing your config in xml if you want interoperability. Every modern programming language has tons of XML parsers available or build-in. Also you gain more flexibility in your settings structure. And you can easily make you settings format typesafe.

0

精彩评论

暂无评论...
验证码 换一张
取 消