PHP'use'关键字如何工作(在Symfony 2中)? [英] How does the PHP 'use' keyword work (in Symfony 2)?

查看:59
本文介绍了PHP'use'关键字如何工作(在Symfony 2中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Symfony 2中,所有请求都通过app_dev.php(或app.php)进行路由.

前几行如下:

<?php

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Debug\Debug;
...

从理论上讲,我知道这只是将指定的名称空间(或类)导入到当前作用域中.我不明白的是PHP如何将其映射到文件.例如,Debug类位于:

vendor/symfony/symfony/src/Symfony/Component/Debug/Debug.php

PHP如何知道在vendor/symfony/symfony/src/中的外观?

我可能误会了正在发生的事情,但是任何澄清都将不胜感激.

解决方案

知道类所在的文件不是语言的工作,而是自动加载器的工作.

所有use关键字所做的只是在此实例中创建别名:

use Symfony\Component\HttpFoundation\Request;

这是在以下脚本中所说的,当我指的是Request时,我真的是指Symfony\Component\HttpFoundation\Request.如果我以某种方式使用Request对象(通过创建实例或在其上调用静态方法),则自动加载器将去尝试加载定义了该类的文件(如果尚未加载的话). /p>

自动加载器的内部工作因项目而异.已经采取了标准化自动装带器行为的动作àla PSR- 4 ,但是该语言中没有任何内容表明您必须遵守该标准.例如,您可以采用一种方案,其中 all 类文件驻留在单个目录中,而自动加载器仅从那里加载所有类,而不管它们位于哪个名称空间中.

该方案将遭受以下事实的困扰:您只能拥有一个每个名字的类,但是没有什么可以阻止您这样做.

回答您的问题:如何知道"Symfony \ Component \ Debug \ Debug"是有效的名称空间?"

没有,因为此时实际上并没有退出并尝试加载该类.如果在任何随机的PHP脚本中,您都将以下内容放在顶部:

use \Non\Existant\ObjectClass;

但是永远不要在任何地方使用 ObjectClass,您不会有任何错误.但是,如果尝试说new ObjectClass,则会收到未找到类的错误.

In Symfony 2, all requests are getting routed through app_dev.php (or app.php).

The first few lines look like:

<?php

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Debug\Debug;
...

In theory, I get that this just imports the specified namespace (or class) to the current scope. What I don't understand is how PHP maps this to a file. For example, the Debug class is located in:

vendor/symfony/symfony/src/Symfony/Component/Debug/Debug.php

How does PHP know to look in vendor/symfony/symfony/src/?

I'm probably misunderstanding what is happening, but any clarification would be appreciated.

解决方案

Knowing which file a class lives in isn't the job of the language, it's the job of the autoloader.

All the use keyword does is this instance is create an alias:

use Symfony\Component\HttpFoundation\Request;

This is saying in the following script, when I refer to Request I really mean Symfony\Component\HttpFoundation\Request. If I use Request object in some way (by either creating an instance or calling a static method on it) then the autoloader will go and try to load the file that class is defined in if it hasn't been loaded already.

The inner workings of the autoloader, though, is different from project to project. There has been a move to standardize autoloader behavior à la PSR-4, but there's nothing in the language saying that you have to adhere to that standard. You could, for instance, have a scheme where all class files reside in a single directory and your autoloader just loads all classes from there, regardless of what namespace they're in.

That scheme would suffer from the fact that you could only have a single class of every name, but there's nothing stopping you from doing it that way.

To answer your question: "How does it know that "Symfony\Component\Debug\Debug" is a valid namespace?"

It doesn't, because it's not actually going out and trying to load that class at this point. If in any random PHP script you put something like this at the top:

use \Non\Existant\ObjectClass;

But don't ever actually use ObjectClass anywhere, you will get no errors. If you try to say new ObjectClass, though, you will get a class not found error.

这篇关于PHP'use'关键字如何工作(在Symfony 2中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆