DiscoInfiltrator
DiscoInfiltrator

Reputation: 2059

How does the PHP 'use' keyword work (in Symfony 2)?

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.

Upvotes: 1

Views: 280

Answers (2)

Jeff Lambert
Jeff Lambert

Reputation: 24661

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.

Upvotes: 3

Populus
Populus

Reputation: 7680

Simplistically speaking, you have to load all the files beforehand into memory for PHP. PHP does not inherently have any standards to where files are located, the rules for loading files has to be done by your code.

When PHP attempts to load a file, it will check its memory if the class definition exists, if not it will attempt to autoload the file that may contain your class definition; see PHP: Autoloading Classes and spl_autoload_register

Some common autoloading strategies have been defined by the PHP Framework Interop Group:

  • PSR-0 (Autoloading standard)
  • PSR-4 (Improved Autoloading standard)

In this case, autoloading is done by Composer, all you need to do is include vendor/autoload.php in any of your scripts and vendor code downloaded via composer can be autoloaded. You can manually add classes into the composer autoloader as well. See: Composer Autoloading

Upvotes: 0

Related Questions