简体   繁体   English

PHP:在include中包含

[英]PHP: include inside include

In dbc.php I have where the connection to the mysql db gets made, and all my functions is there. 在dbc.php中,我可以在其中建立与mysql db的连接,并且所有功能都在那里。

dbc.php is included on all my files. dbc.php包含在我的所有文件中。

Now all functions are in dbc.php and i would like to sort out alittle. 现在所有功能都在dbc.php中,我想整理一下。 I was thinking about putting all form_*() functions to form_api.php, and then include it in dbc.php. 我正在考虑将所有form _ *()函数放入form_api.php,然后将其包含在dbc.php中。 But will this work? 但这行得通吗? Can i just include inside a file i included? 我可以只在我包含的文件中添加吗? Or should i use require/include_once ? 或者我应该使用require / include_once吗?

help me out thank you 帮帮我,谢谢

是的....这是完全允许的。

我建议您对库使用__autoload()方法并将其放入类中。

It should work, just save the form_api.php file in the same folder as your dbc.php file and include it from it. 它应该可以正常工作,只需将form_api.php文件与dbc.php文件保存在同一文件夹中,并从中包含它。 As you probably know, include_once() has the same behaviour but includes a file just once, no matter how many times you try to include it, so it is perfectly OK to use include_once instead of include(). 您可能知道,include_once()具有相同的行为,但是仅包含一个文件,无论您尝试包含它多少次,因此使用include_once代替include()是完全可以的。 require() will stop the execution will a fatal error if the file can't be included for any reason. 如果由于某种原因无法包含该文件,则require()将停止执行,这将是一个致命错误。

You can use include() functions at included pages. 您可以在包含的页面上使用include()函数。 So your first questions answer is YES . 因此,您的第一个问题的答案是“是”。

And now 2. question. 现在2.问题。 Actually , there is an different between include and include_once . 实际上, includeinclude_once之间是有区别的。

include("dbc.php");
include("dbc.php");
include("dbc.php");
// dbc.php will include 3 times .

include_once("dbc.php");
include_once("dbc.php");
include_once("dbc.php");
// dbc.php will include 1 time

So, i prefer include_once :) 所以,我更喜欢include_once :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM