search
HomeBackend DevelopmentPHP Tutorialuse语句必须放在函数外面的理由是什么?

下面为何不可以

<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)就被处理。

和运行时才现场执行(相当于语句)的echorequire等不一样,use语言结构是在解析(parse)阶段预先扫描、提早处理的。以上是前提。


解析操作本身,非常的单纯,仅仅是从头推到尾,识别一个个的语言关键字,并确保语法规则不被违反。我们可以做一个简单的试验:

<code><?php echo '可爱的紫妹纸是永远的 17 岁!';
die();
{
    我就是叫紫妈怎么了?有本事突然从我背后出现,把我的脸按在键盘上5rt4n7tojd87tg2435t4q34
}</code></code>

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

use语句必须放在函数外面的理由是什么?


但对于一个花括号括住的作用域(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这样,从最初的设计目的,从而推导出其设计必然限制的语法糖,其实挺少的。

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

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools