PHP 中 require_once 的范围是什么?

发布于 2024-08-30 00:32:55 字数 577 浏览 9 评论 0原文

简单的问题: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 技术交流群。

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

发布评论

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

评论(4

独闯女儿国 2024-09-06 00:32:55

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 calling require_once() in, it's whether or not that physical file has previously been required.

In your code above, your foo() function would not re-parse baz.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 when require_once() does succeed.

短暂陪伴 2024-09-06 00:32:55

事实并非如此。 require_once 的跟踪适用于内部函数。
但是,以下脚本会产生错误:

a.php

<?php
require_once('b.php');
function f() { require_once('b.php'); echo "inside function f;"; }
?>

b.php,

<?php
f();
?>

因为函数 f() 未预定义到 b.php

It does not. require_once's tracking applies to inside functions.
However, the following scripts produce an error:

a.php

<?php
require_once('b.php');
function f() { require_once('b.php'); echo "inside function f;"; }
?>

b.php

<?php
f();
?>

because function f() is not pre-defined to b.php.

囍孤女 2024-09-06 00:32:55

为了更具体地回答您的问题,当您第二次对该文件调用 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 of require_once if you want it to be included again, thereby redefining the variables in your scope.

我是有多爱你 2024-09-06 00:32:55

至少在 PHP 7.3 中,require_once 具有全局范围。
如果不是,x.phpz.php 都应该抛出错误,就像 y.php 那样:

a.php

<?php function a() { require_once 'b.php'; b(); }

b.php

<?php function b() { debug_print_backtrace(); }

x .php

<?php
require_once 'a.php';
a();
b();

-->

#0 c() called at [/var/www/b.php:2]  
#1 b() called at [/var/www/a.php:3]  
#0 c() called at [/var/www/a.php:4]  

y.php

<?php
require_once 'a.php';
b();

--> 上调用 /var/www/y.php 中未定义的函数 b()

致命错误:未捕获错误:在第 3 行z.php

<?php
require_once 'a.php';
require_once 'b.php';
b();

-->

#0 b() called at [/var/www/z.php:4]

At least in PHP 7.3, require_once has global scope.
If it would not, both x.php and z.php should throw errors as y.php does:

a.php

<?php function a() { require_once 'b.php'; b(); }

b.php

<?php function b() { debug_print_backtrace(); }

x.php

<?php
require_once 'a.php';
a();
b();

-->

#0 c() called at [/var/www/b.php:2]  
#1 b() called at [/var/www/a.php:3]  
#0 c() called at [/var/www/a.php:4]  

y.php

<?php
require_once 'a.php';
b();

--> Fatal error: Uncaught Error: Call to undefined function b() in /var/www/y.php on line 3

z.php

<?php
require_once 'a.php';
require_once 'b.php';
b();

-->

#0 b() called at [/var/www/z.php:4]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文