开源软件 - 如何设置文件
我正在制作自己的自定义论坛系统软件。很像 phpbb、mybb、vbulletin 等,但它显然不太先进。这只是我自己的一个个人项目,我遇到了一些问题,因为我从来没有开发过可以为其他人重新打包的东西。
文件结构如下:
所以,config.php 是所有包含文件的最后。它具有数据库连接信息,它还实例化了我的数据库类,并且所有函数文件都不需要/包含任何文件,因为它们总是在需要 config.php 的地方被访问。
这是问题! 然而,我遇到了简单但非常烦人的问题,例如,我在 config.php 的顶部调用一个函数,该函数检查用户的 cookie 值并确保它们都属于同一用户,如果不是,则会删除 cookie。但是,它必须在数据库文件需要之后。诸如 config.php 中声明的变量并不总是可访问的,因此有时我必须在头文件中声明它。
看起来这并不是什么大问题,但我想这只是问我如何在一般情况下包含/需要而不遇到问题。
I'm in the middle of making my own custom forum system software. Much like phpbb, mybb, vbulletin, etc. except it's obviously quite less advanced. It's just a personal project for myself and I've run into some problems since I've never had to develop something that can be repackaged for others.
The file structure is as follows:
So, config.php is the end all be all of including files. It has the database connection information, it instantiates my database class as well, and none of the function files require/include any files since they'll always be accessed where config.php is required.
HERE'S THe QUESTION!
However I'm running into simple but very annoying problems, for example I call a function in config.php towards the top that checks the users cookies values and makes sure they all belong to the same user, and if not it deletes the cookies. However, it has to be after the database files require. And things like, a variable declared in config.php isn't always accessible, so sometimes I have to declare it in the header files.
Seems like it's not much of a question, but I guess it's just asking for how I can include/require in general without running into issues.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般而言,大多数人不会将配置变量和代码混合在一个文件中。如果您查看像 Wordpress 这样的流行开源软件包,您会发现 Config.php 只是设置了配置变量。没有代码。
As a general note, most people don't mix config variables and code in one file. If you look at popular open source packages like Wordpress, Config.php just has config variables set. No code.
如果您在不仅仅是“一次性”情况下使用某些函数,您可能需要考虑将它们放入您的主类中 - 这样它们就可以根据需要使用。
@James 是对的,分开你的配置文件。您可以将其包含在“application.php”所需文件中(因此它可以在全球范围内使用)。
我遇到过这样的情况:在页面构建之前我绝对需要 HTTP 标头信息。虽然看起来有点落后,但解决方案是先调用该文件,然后包含 application.php 文件。检查 cookie 应该没问题。
在另一种情况下,@include('myStubbonPricing.php') 就是答案。我不是错误抑制的倡导者,但就我而言,它只输出运费(如果输入了邮政编码)。我要辩解的是,由于 XML 请求/响应场景,!isset 等不会解决问题。
If you're using certain functions in anything more than a "one off" situation, you may want to consider putting them into your main class - that way they're available as needed.
@James is right, separate your config file. You can include it inside an "application.php" required file (so it's available globally).
I have run into a situation where I absolutely needed HTTP Header information prior to page build. Though it seemed a little backward, the solution was to call that file first, then include the application.php file. Checking for a cookie should be fine.
In another situation, @include('myStubbonPricing.php') was the answer. I'm not an advocate of error suppression, but in my case it only outputted a shipping rate (if the zip code was entered). To my defense !isset and the like would not fix the problem due to an XML request/response scenario.