包括、要求和需要一次

发布于 2024-09-04 05:23:27 字数 1282 浏览 7 评论 0原文

今天我尝试包含返回对象的文件。我总是使用 require_once,但是现在我注意到它的奇怪行为。

文件main.php

$lang = false;
$lang->name = "eng";
$lang->author = "Misiur";
$lang->text = "Text is test";
$lang->undefined = "Undefined";
return $lang;

文件index.php

$lang = include('langs/eng/main.php');
var_dump($lang);
echo "<br />";
$lang = require('langs/eng/main.php');
var_dump($lang);
echo "<br />";
$lang = require_once('langs/eng/main.php');
var_dump($lang);

结果

object(stdClass)#9 (4) { ["name"]=>  string(3) "eng" ["author"]=>  string(6) "Misiur" ["text"]=>  string(12) "Text is test" ["undefined"]=>  string(9) "Undefined" }
object(stdClass)#10 (4) { ["name"]=> string(3) "eng" ["author"]=> string(6) "Misiur" ["text"]=> string(12) "Text is test" ["undefined"]=> string(9) "Undefined" }
bool(true) 

为什么会这样呢?我认为 require 和 require_once 是同一件事,只有 require_once 更安全,因为它不会重复包含。

谢谢。

编辑:

但是当我使用 only require_once 时,我也得到 bool(true) 。那么 require_once 只返回包含的结果,而不返回它的内容?

编辑2:

哈哈。我没有注意到,早些时候我在我的类中需要这个文件,该文件是在代码执行之前创建的($this->file = require_once("langs/$name/main.php");)

所以 require_once 起作用了正如它应该的那样。谢谢你们!

Today I've tried to include file that returns object. I always use require_once, however now I've noticed weird behavior of it.

File main.php

$lang = false;
$lang->name = "eng";
$lang->author = "Misiur";
$lang->text = "Text is test";
$lang->undefined = "Undefined";
return $lang;

File index.php

$lang = include('langs/eng/main.php');
var_dump($lang);
echo "<br />";
$lang = require('langs/eng/main.php');
var_dump($lang);
echo "<br />";
$lang = require_once('langs/eng/main.php');
var_dump($lang);

Result

object(stdClass)#9 (4) { ["name"]=>  string(3) "eng" ["author"]=>  string(6) "Misiur" ["text"]=>  string(12) "Text is test" ["undefined"]=>  string(9) "Undefined" }
object(stdClass)#10 (4) { ["name"]=> string(3) "eng" ["author"]=> string(6) "Misiur" ["text"]=> string(12) "Text is test" ["undefined"]=> string(9) "Undefined" }
bool(true) 

Why is it like that? I thought that require and require_once are same thing, only require_once is more safe because it won't duplicate include.

Thanks.

Edit:

But when i use only require_once, I get bool(true) too. So require_once returns only result of include, not it's content?

Edit2:

LOL. I haven't noticed, that earlier I had required this file inside of my class which is created before this code execution ($this->file = require_once("langs/$name/main.php");)

So require_once worked as it should. Thanks guys!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

一场春暖 2024-09-11 05:23:27

当您使用 include 和 require 时,您会得到您引用的页面被包含的结果。当您使用 require_once 时,它​​会检查并发现该页面已使用 require 加载,因此它返回 true 告诉您它在某个时刻已成功加载。

When you use include and require you get the result of the page you're referencing being included. When you use require_once it checks and sees that the page has already been loaded using require, so it returns true to tell you that it has been loaded successfully at some point.

窝囊感情。 2024-09-11 05:23:27

正是如此,您正在尝试复制包含。当您 require_once 之前未包含或需要的内容时,您将获得返回值。

编辑:通过 PHP 5.3.2 上的这个简单测试,我在第一次使用 require_once 时得到了返回值。

Parent.php:

<?php
$first = require_once("child.php");
var_dump($first);
$second = require_once("child.php");
var_dump($second);
?>

child.php:

<?php
return "foo";
?>

它打印:

string(3) "foo"
bool(true)

That's exactly it, and you're trying to duplicate include. When you require_once something that hasn't been included or required before, you will get the return value.

EDIT: With this simple test on PHP 5.3.2, I get the return value when using require_once for the first time.

parent.php:

<?php
$first = require_once("child.php");
var_dump($first);
$second = require_once("child.php");
var_dump($second);
?>

child.php:

<?php
return "foo";
?>

It prints:

string(3) "foo"
bool(true)
掌心的温暖 2024-09-11 05:23:27

我假设您在这两个文件上都有打开和关闭标签?关于 include 的 PHP 文档提到了其中的一些内容。查看该页面上的示例#5。

看起来 includerequirerequire_once 都具有相同的功能,只是 require 发出错误而不是警告,而 require_once 则不会发出错误t 重复包含文件。

I'm assuming that you do have open and closing tags on both files? The PHP documentation on include mentions some of this. Check out example #5 on that page.

It looks like include, require, and require_once all have the same functionality, just that require emits an error instead of warning, and require_once doesn't duplicate-include a file.

捂风挽笑 2024-09-11 05:23:27

切勿将 return 与包含一起使用。
不返回 anting 也不分配任何变量。

Never use return with includes.
Do not return anting and do not assign any variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文