module_load_include() 与 require_once
我想知道何时需要使用 module_load_include()
或 require_once
来包含位于模块内的文件。
I was wondering when you need to use module_load_include()
or require_once
to include files which are located within your module.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Drupal
module_load_include()< 的关键是/code>
函数的功能超出了标准 PHP
require_once
是它在定位文件时引用模块的路径,使用drupal_get_path()
。如果您要使用
require_once
,您必须自己做这件事。它所做的另一件事是在尝试包含该文件之前检查该文件是否存在,这对于避免致命崩溃很方便,但如果您在尝试调用尝试包含的函数时无论如何都会得到一个文件,则毫无意义。不过,这很方便,可以让您产生更有意义的错误。
一天结束时,
module_load_include()
实际上只是 Drupal 提供的一个小实用函数,目的是使事情变得稍微容易一些。如果您知道该文件所在的位置,并且知道该文件存在于该位置,则几乎不需要使用 Drupal 功能;您也可以使用require_once
。The key thing that the Drupal
module_load_include()
function does over and above the standard PHPrequire_once
is that it references the module's path when locating the file, usingdrupal_get_path()
.If you were to use
require_once
, you would have to do this bit yourself.The other thing it does is check that the file exists prior to trying to include it, which is handy for avoiding fatal crashes, but rather pointless if you're going to get one anyway when you try to call the functions you tried to include. This is handy though for allowing you to produce more meaningful errors.
At the end of the day,
module_load_include()
is really just a little utility function provided by Drupal to make things slightly easier for themselves. If you know where the file is located, and you know it exists there, there's very little need to use the Drupal function; you may just as well userequire_once
.module_load_include 要求 Drupal 完全加载(完全引导)。
语法: module_load_include($type, $module, $name = NULL);
例如:
module_load_include('inc','module_name','file_name');
如果您想在全局上下文中使用此函数,请使用 require_once
require_once< /strong> 不需要它。
例如:require_once DRUPAL_ROOT。 '/路径/文件' .
module_load_include requires Drupal to be loaded fully (Fully Bootstrapped).
syntax: module_load_include($type, $module, $name = NULL);
Eg:
module_load_include('inc','module_name','file_name');
if u want to use this function in a global context then use require_once
require_once doesn't need it.
Eg:
require_once DRUPAL_ROOT . '/path/file' .