use语句必须放在函数外面的理由是什么?
下面为何不可以
<code>function done() { require_once 'vendor/autoload.php'; use Qiniu\Auth; use Qiniu\Storage\UploadManager; #省略 }</code>
?>
必须写成
<code>require_once 'vendor/autoload.php'; use Qiniu\Auth; use Qiniu\Storage\UploadManager; function done() { #省略 }</code>
?>
回复内容:
下面为何不可以
<code>function done() { require_once 'vendor/autoload.php'; use Qiniu\Auth; use Qiniu\Storage\UploadManager; #省略 }</code>
?>
必须写成
<code>require_once 'vendor/autoload.php'; use Qiniu\Auth; use Qiniu\Storage\UploadManager; function done() { #省略 }</code>
?>
语言结构使然,如果你了解PHP命名空间相关性质我想你是不会这么问的。
use
的作用仅仅是用短名称替代长名称,或者是用别名替代本名,是一个没有语义和实效的“语法糖”。
所以消灭use
的运行时开销是一个非常合理的选择。因此php规定use
在解析阶段(parse)就被处理。
和运行时才现场执行(相当于语句)的echo
、require
等不一样,use
语言结构是在解析(parse)阶段预先扫描、提早处理的。以上是前提。
而解析操作本身,非常的单纯,仅仅是从头推到尾,识别一个个的语言关键字,并确保语法规则不被违反。我们可以做一个简单的试验:
<code><?php echo '可爱的紫妹纸是永远的 17 岁!'; die(); { 我就是叫紫妈怎么了?有本事突然从我背后出现,把我的脸按在键盘上5rt4n7tojd87tg2435t4q34 }</code></code>
我不知道紫妈会不会用我的脸滚键盘,但我知道php肯定不会让我过解析——
你说第 5 行永远都不会执行?解析器根本不知道,也不关心。
但对于一个花括号括住的作用域(scope)而言,事情就变得复杂了。因为一个小作用域的执行顺序很可能是乱的——可以回头、可以通过调用来乱跳等等。例如:
<code>namespace NS1; class ClassName { } function f() { return new ClassName(); } for ($i=0; $i</code>
如果我们认为use
会影响它后边的所有内容,那么此时$a
和$b
的赋值语句到底在不在use
的后边?
按照语义,第1次循环不在,第2次循环在,也就是说同一行会产生两种不同的语义。
但解析器不可能理解,也不可能维护得了这种逻辑。实现这种逻辑,必然产生一个运行时的开销(因为要介入程序运行当时才能确定的状态),而这是use
的设计本意要避免的。
所以use
只能摆在文件的最外层作用域中。只有这个作用域的范围是一线平推,不可能回退,也不可能出现跳转。
试分析以下use
真正的作用范围,就可以看到逻辑中,处处都是为了方便解析器处理而设计的:
从
use
出现的行开始(简单的开始规则)见到
namespace
结束(简单的终止规则)见到文件尾结束(解析器的运行不能跨文件)
事实上和严谨设计、环环相扣的语言特性不同,很多的语法糖都并没有太多的道理可讲。
能像use
这样,从最初的设计目的,从而推导出其设计必然限制的语法糖,其实挺少的。
对于语法糖,死记、活用、理解原理但别想太多,这才是我们作为语言使用者的营生之道。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
