为什么返回的文件中的方法仍然有效?

时间:2017-05-13 13:04:42

标签: php

如果您有两个文件,file-a.phpfile-b.php

file-a.php内容:

require 'file-b.php';
my_test( 'hi' );

file-b.php内容:

function my_test( $text ) {
  echo $text; 
}

然后"嗨"正如预期的那样回应。

但如果在file-b.php中你在第一行添加一个回报,就像这样:

return;
function my_test( $text ) {
  echo $text; 
}

然后没有什么是回声,但也没有错误通知说" my_test"方法没有在任何地方声明。 为什么会这样?我期待一个未声明的功能错误。

编辑:澄清。如果我然后制作file-a.php内容:

require 'file-b.php';
my_test( 'hi' );
my_second_test_that_doesnt_exist( 'hi' );

然后我确实接到了对未定义功能的调用" my_second_test_that_doesnt_exist"'

1 个答案:

答案 0 :(得分:0)

您正从文件中返回。所以一切都在线下"返回"没有被执行。但它会被PHP注意到,所以即使你不能调用它,PHP也知道它存在,所以它不会抛出错误。