Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

php - 模拟错误以区分PHP中的include和require(simulate errors to differentiate between include and require in PHP)

I've read about how require and include differ In short, one gives a warning an other quits process execution.

(我已经读过关于require和include 差异的内容 。简而言之,一个发出警告,另一个退出流程执行。)

I'd like to simulate the behaviour to see the difference live in a non production environment.

(我想模拟行为,以查看非生产环境中的差异。)

I've created two classes in separate files:

(我在单独的文件中创建了两个类:)

namespace includeTest;
class IncludeError{
    public static function errorTrigger(){
        throw new Exception();
    }
}

and

(和)

namespace requireTest;
class RequireError{
    public static function errorTrigger(){
        throw new Exception();
    }
}

When I test the IncludeError::errorTrigger() with this code

(当我使用此代码测试IncludeError::errorTrigger()时)

namespace index;
include "includeTest.php";
includeTestIncludeError::errorTrigger();
print "done in index.php";

I am getting the usual Fatal error: Uncaught Exception and the "done in index.php" line never prints, indicating that even though the error (actually Exception) happened in "includeTest.php", it was terminating the caller script as well.

(我遇到了通常的Fatal error: Uncaught Exception"done in index.php"行从不打印,这表明即使错误(实际上是Exception)发生在“ includeTest.php”中,它也正在终止调用者脚本。)

Clearly exceptions are not the type of errors that give warnings when included .

(显然,异常不是被included时发出警告的错误类型。)

What kind of error would trigger just a warning?

(什么样的错误只会触发警告?)

I am using php 7.3.8.

(我正在使用PHP 7.3.8。)

An excerpt from the book I am reading:

(我正在阅读的书摘录:)

The only difference between the include() and require() statements lies in their handling of errors.

(include()和require()语句之间的唯一区别在于它们对错误的处理。)

A file invoked using require() will bring down your entire process when you meet an error.

(遇到错误时,使用require()调用的文件将使您的整个过程中断。)

The same error encountered via a call to include() will merely generate a warning and end execution of the included file, leaving the calling code to continue.

(通过调用include()遇到的相同错误仅会生成警告并结束包含文件的执行,而使调用代码继续进行。)

This makes require() and require_once() the safe choice for including library files, and include() and include_ once() useful for operations like templating.

(这使得require()和require_once()成为包括库文件的安全选择,而include()和include_ Once()对于诸如模板之类的操作很有用。)

  ask by sanjihan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...