开发者

catch a custom exception - do we need to require or include the CustomException class?

To th开发者_StackOverflowrow an exception we need to include a CustomException class.

include_once("CustomException.class.php");

Ok.

On another file, when we use the try/catch blocks, do we need to require or include, our CustomException class again?

Thanks in advance, MEM


If the exception of this class was thrown, it has already been included at this point of the script. And if it's not thrown nothing bad happens. The typehint will not raise any errors if there's no such exception defined. Try running this code:

try {
 echo 'foo';
} catch (SomeNonExistentException $e) {
 echo 'bar';
}
echo 'baz';


No, you do not need to re-include it in the try/catch blocks. Once a file is included it should be valid / available for the entire processing of the script.


If I understand what you're trying to do correctly, then no, because when you include the file that has the class that throws CustomException, it will include the CustomException class already.


Consider the following situation, where we have a main file which includes a file (which includes a file itself):

main.php:

include("include1.php");
var_dump($variable_defined_in_include2);

include1.php:

include("include2.php");

include2.php:

$variable_defined_in_include2 = true;

Even though main.php doesn't include include2.php, $variable_defined_in_include2 will be set because include1.php is included, which includes include2.php.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜