Why must the use statement be placed outside the function?

Source: Internet
Author: User
Tags autoload
Why not & amp; lt ;? Php {code ...}? & Amp; gt; must be written as & amp; lt ;? Php {code ...}? & Amp; gt; why not
Function done () {require_once 'vendor/autoload. php'; use Qiniu \ Auth; use Qiniu \ Storage \ UploadManager; # omitted}

?>

Must be written

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

?>

Reply content:

Why not?

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

?>

Must be written

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

?>

Because of the language structure, if you know the nature of the PHP namespace, I don't think so.

useThe function is only to replace a long name with a short name, or to replace the original name with an alias.Syntactic sugar".

So eliminateuseIs a very reasonable choice. Therefore, php requiresuseIt is processed in the parsing phase (parse.

Execute on-site (equivalent to a statement) only when runningecho,requireAnd so on,useThe language structure is pre-scanned and pre-processed in the parsing (parse) phase.. The above is the premise.

WhileAnalysisThe operation itself is very simple. It only starts from the beginning to the end, recognizes language keywords one by one, and ensures that the syntax rules are not violated. We can do a simple experiment:


  

I don't know if Zima will use my face to roll the keyboard, but I know that php won't let me parse it --
Do you say 5th rows will never be executed? The parser does not know or care about it.

But for a scope enclosed by curly braces, things become complicated. Because the execution sequence of a small scope is likely to be messy-you can go back, you can jump through the call, and so on. For example:

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();}

If we thinkuseWill affect all the content behind it, then at this time$aAnd$bThe value assignment statement is not inuseBehind?

According to the semantics, 1st cycles are not there, and 2nd cycles are there. That is to say, the same row produces two different semantics.

However, the parser cannot understand or maintain this logic. To implement this logic, it will inevitably generate a runtime overhead (because it is necessary to intervene in the status that can be determined only when the program is running). This isuseDesigned to avoid.

SouseIt can only be placed in the outermost scope of the file. Only the scope of this scope is a one-line push. It is impossible to roll back or jump.

Try to analyze the followinguseThe true scope can be seen that in the logic, everything is designed to facilitate the parser processing:

  • SlaveuseStart of the row that appears (simple start rule)

  • SeenamespaceEnd (simple termination rule)

  • End at the end of the file (the parser cannot run across files)

In fact, it is similar to rigorous design and interlockingLanguage FeaturesDifferent, manySyntactic sugarThere is not much truth to explain.

Energy ImaginguseIn this way, from the initial design purpose, it is quite rare to deduce the syntactic sugar that is inevitably limited by its design.

For syntactic sugar, this is what we use as a language user.

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.