What is the reason why the USE statement must be placed outside the function?

Source: Internet
Author: User
Tags autoload
Below is why you can
function done() {    require_once 'vendor/autoload.php';    use Qiniu\Auth;    use Qiniu\Storage\UploadManager;    #省略    }

?>

Must be written

require_once 'vendor/autoload.php';use Qiniu\Auth;use Qiniu\Storage\UploadManager;function done() {    #省略    }

?>

Reply content:

Below is why you can

function done() {    require_once 'vendor/autoload.php';    use Qiniu\Auth;    use Qiniu\Storage\UploadManager;    #省略    }

?>

Must be written

require_once 'vendor/autoload.php';use Qiniu\Auth;use Qiniu\Storage\UploadManager;function done() {    #省略    }

?>

The language structure dictates that if you understand PHP namespace-related properties I don't think you would ask.

useis to replace the long name with a short name, or to replace it with an alias, which is a " syntactic sugar " that has no semantic or practical effect.

So the use run-time overhead of elimination is a very reasonable choice. So PHP rules are use processed in the parsing phase (parse).

The echo require use language structure is pre-scanned and processed early in the parse phase , unlike when the runtime executes (equivalent to a statement) in the field. These are the prerequisites.

The parsing operation itself, is very simple, just push from the beginning to the end, identify the language keyword, and ensure that the grammar rules are not violated. We can do a simple experiment:


  
   

我不知道紫妈会不会用我的脸滚键盘,但我知道php肯定不会让我过解析——
你说第 5 行永远都不会执行?解析器根本不知道,也不关心。

但对于一个花括号括住的作用域(scope)而言,事情就变得复杂了。因为一个小作用域的执行顺序很可能是乱的——可以回头、可以通过调用来乱跳等等。例如:

namespace NS1;class ClassName { }function f() { return new ClassName();}for ($i=0; $i<2; $i++) { $a = new ClassName(); $b = f(); use NS2\ClassName; $c = new ClassName();}

如果我们认为use会影响它后边的所有内容,那么此时$a$b的赋值语句到底在不在use的后边?

按照语义,第1次循环不在,第2次循环在,也就是说同一行会产生两种不同的语义。

但解析器不可能理解,也不可能维护得了这种逻辑。实现这种逻辑,必然产生一个运行时的开销(因为要介入程序运行当时才能确定的状态),而这是use的设计本意要避免的。

所以use只能摆在文件的最外层作用域中。只有这个作用域的范围是一线平推,不可能回退,也不可能出现跳转。

试分析以下use真正的作用范围,就可以看到逻辑中,处处都是为了方便解析器处理而设计的:

    • use出现的行开始(简单的开始规则)

    • 见到namespace结束(简单的终止规则)

    • 见到文件尾结束(解析器的运行不能跨文件)

事实上和严谨设计、环环相扣的语言特性不同,很多的语法糖都并没有太多的道理可讲。

能像use这样,从最初的设计目的,从而推导出其设计必然限制的语法糖,其实挺少的。

对于语法糖,死记、活用、理解原理但别想太多,这才是我们作为语言使用者的营生之道。

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.