使用 PHP 解析 *.ini 文件时丢失布尔值
只是尝试利用本机 PHP parse_ini_file
函数在 PHP 中创建我自己的配置处理程序。我确实注意到,所有值为布尔值的配置条目 - true/false 都被搞乱了,并且大多数其他配置条目都被类型转换为字符串。
显然,这已经是一个众所周知的问题: http: //www.php.net/manual/en/function.parse-ini-file.php#100851
那么适当的解决方法是什么?
利用非 PHP 配置确实很巧妙,这使得非开发人员在必要时可以轻松更改值,因此我很乐意保留基于 *.ini
的配置。
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, 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有点晚了,但从 PHP 5.6 开始,您可以传递第三个参数 INI_SCANNER_TYPED ,它将维护值的类型
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我想解决这个问题的最简单的方法就是使用您自己的配置处理程序。要么编写一个 ini 类,要么下载一个免费的(ini 格式非常简单)。
但如果您想要互操作性,我建议您将配置存储在 xml 中。每种现代编程语言都有大量可用或内置的 XML 解析器。您还可以在设置结构中获得更大的灵活性。并且您可以轻松地使设置格式类型安全。
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.