PHP 中 require_once 的范围是什么?
简单的问题:require_once 的范围是全局的吗?
例如:
<?PHP
require_once('baz.php');
// do some stuff
foo ($bar);
function foo($bar) {
require_once('baz.php');
// do different stuff
}
?>
当 foo 被调用时,它会重新解析 baz.php 吗? 或者它是否依赖于主 php 文件中已经需要的文件(类似于对同一个包含文件连续调用 require_once 两次) )?
我之前看过这个帖子,但它并没有完全回答问题:
应该 require_once "some file.php" ;出现在文件顶部以外的任何地方?
感谢您的帮助!
Simple question: Is the scope of require_once
global?
For example:
<?PHP
require_once('baz.php');
// do some stuff
foo ($bar);
function foo($bar) {
require_once('baz.php');
// do different stuff
}
?>
When foo is called, does it re-parse baz.php? Or does it rely on the already required file from the main php file (analagous to calling require_once twice consecutively for the same include file)?
I saw this thread before, but it didn't quite answer the question:
Should require_once "some file.php" ; appear anywhere but the top of the file?
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
require_once() 基本上依赖于物理文件来确定它是否已被包含。因此,与其说您在其中调用
require_once()
的上下文,不如说是之前是否需要该物理文件。在上面的代码中,您的
foo()
函数不会重新解析baz.php
,因为它将与之前包含在顶部的文件相同。但是,根据是将其包含在
foo()
内还是将其包含在顶部,您会得到不同的结果,因为当require_once()
成功时,作用域将应用。require_once()
basically relies on the physical file to determine whether or not it's been included. So it's not so much the context that you're callingrequire_once()
in, it's whether or not that physical file has previously been required.In your code above, your
foo()
function would not re-parsebaz.php
, since it is going to be the same file as was previously included at the top.However, you will get different results based on whether you included it inside
foo()
, or included it at the top, as the scoping will apply whenrequire_once()
does succeed.事实并非如此。
require_once
的跟踪适用于内部函数。但是,以下脚本会产生错误:
a.php
b.php,
因为函数 f() 未预定义到
b.php
。It does not.
require_once
's tracking applies to inside functions.However, the following scripts produce an error:
a.php
b.php
because function f() is not pre-defined to
b.php
.为了更具体地回答您的问题,当您第二次对该文件调用
require_once
时,它不会执行任何操作,因为它已经包含在内。如果您的包含文件中有函数等,那么您无论如何都会遇到将其包含在函数内的问题,因此范围是无关紧要的。如果只是定义或处理变量,那么如果您希望再次包含它,则可以使用
require
而不是require_once
,从而重新定义作用域中的变量。To more specifically answer your question, the second time you call
require_once
on that file, it won't do anything, because it's already be included.If your include has functions etc. in it, then you would have issues including it inside a function anyway, so scope is irrelevant. If it's just variables being defined or processed, then you can just use
require
instead ofrequire_once
if you want it to be included again, thereby redefining the variables in your scope.至少在 PHP 7.3 中,
require_once
具有全局范围。如果不是,
x.php
和z.php
都应该抛出错误,就像y.php
那样:a.php
b.php
x .php
-->
y.php
--> 上调用 /var/www/y.php 中未定义的函数 b()
致命错误:未捕获错误:在第 3 行z.php
-->
At least in PHP 7.3,
require_once
has global scope.If it would not, both
x.php
andz.php
should throw errors asy.php
does:a.php
b.php
x.php
-->
y.php
--> Fatal error: Uncaught Error: Call to undefined function b() in /var/www/y.php on line 3
z.php
-->