如何在 Zend_Config 中存储数值而不丢失其类型?

发布于 2024-10-10 05:51:17 字数 179 浏览 0 评论 0原文

我决定将一些应用程序设置从数据库移动到文件系统,以便可以通过 Zend_Config 进行访问。 然而我遇到了一件烦人的事情,Zend_Config 将所有值识别为字符串忽略数据类型。

有没有什么神奇的方法来解析配置文件,保留原始类型,如浮点数、整数、字符串,以及 INI、XML、YAML 或 JSON 的最佳存储方式是什么?

I decided to move some application settings from database to file system to be accessible via Zend_Config.
However I run in to one annoying thing, Zend_Config recognise all values as string ignoring data type.

Is there any magic way to parse config file keeping original types like float, integer, string and what would be best storage for it INI, XML, YAML or JSON?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖上的星空 2024-10-17 05:51:17

至少不可能使用 Zend_Config_Xml

配置数据读入
Zend_Config_Xml 始终返回为
字符串。数据转换自
字符串到其他类型剩下
开发人员根据自己的具体情况
需求。

不确定其他人的情况。你可以做很多类似的事情

// config.php
return array('foo' => 1);

// bootstrap
$config = new Zend_Config(include 'config.php');
var_dump($config->foo); // int(1)

At least not possible with Zend_Config_Xml:

Configuration data read into
Zend_Config_Xml are always returned as
strings. Conversion of data from
strings to other types is left to
developers to suit their particular
needs.

Not sure about the others. You can very much do something like

// config.php
return array('foo' => 1);

// bootstrap
$config = new Zend_Config(include 'config.php');
var_dump($config->foo); // int(1)
叹倦 2024-10-17 05:51:17

Zend 将使用 JSON 和 YAML 自动将整数、浮点数和布尔值转换为各自的类型。 XML 始终是字符串,而 INI 在选择类型时具有 parse_ini_file() 的所有注意事项和功能。

如果您想要显式类型(例如特定对象),则需要将其包含在序列化数据中。

Zend will automagically turn integers, floats, and bools into their respective types with JSON and YAML. XML is always string, and INI has all the caveats and power of parse_ini_file() when it comes to choosing type.

If you want an explicit type, such as a particular object, you need to include that in the serialized data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文