0

I was just playing around with my IDE and I noticed that when I use the "Fix code" option on a class it adds a bunch of lines on the top of the following type

use Someclass;
use \Ano\therClass;
use Iface;
...

I was wondering what exactly is the purpose of this, since the classes are going to be loaded on demand, is there a need to explicitly declare which classes are going to be used?

Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
php_nub_qq
  • 15,199
  • 21
  • 74
  • 144
  • 1
    A use statement with the entire namespace at the top allows you to use the class in the code without the namespace (ie: new therClass). Also it's nice to have them listed at the beginning so you can easily see which dependencies the file / class has. – JimL Oct 04 '14 at 12:29
  • @JimL That sounds like it is going to consume some resources, is that the case? – php_nub_qq Oct 04 '14 at 12:43
  • 1
    All code consumes resources, this namespace-lookup stuff is not something that will take any substantial amount of resources though. Not the right place to spend time trying to improve performance :) – JimL Oct 04 '14 at 12:45
  • Possible duplicate of [What are namespaces?](https://stackoverflow.com/questions/3384204/what-are-namespaces) – Sunny Patel Sep 18 '18 at 19:48

2 Answers2

1

Using use you can basically have different objects, functions, etc. with the same name, thanks to namespaces. When you write use in your code, you tell PHP to import items of a namespace and give it an alias.

Read more: http://php.net/manual/en/language.namespaces.importing.php

  • Yes I know but if I'm not aliasing them it just makes no sense to needlessly make these declarations, or does it? – php_nub_qq Oct 04 '14 at 12:24
  • If you don't plan on using different namespaces in your project then these declarations are unnecessary, but your IDE probably uses it to avoid naming conflicts when developing large-scale projects. –  Oct 04 '14 at 12:33
  • This is exactly what is confusing me. It appears to me more likely to not have a name conflict if you use the full namespace to a given class rather than using aliases and stuff. – php_nub_qq Oct 04 '14 at 12:38
  • Well, with aliasing you can decide whether you want to actually give an alias to a namespace, or not. If you choose not to (like in this case), you can just refer to items of that namespace as if they were defined in the current namespace. But you can also give the imported namespaces an alias. This could be useful if you have many long namespaces, and don't want to write each namespace every time. This could be considered as a shorthand method of using namespaces with long names. At least that's what I've been using aliases for. –  Oct 04 '14 at 12:56
0

When U create a class, name it's package with keyword namespace

<?php
/**
 * CacheException.php
 */
namespace Doctrine\ORM\Cache;

class CacheException extends Exception {}

Elsewhere use can import only one class from package:

use Doctrine\ORM\Cache\CacheException;

throw new CacheException('Failed to cache');

Also use imports overal package with all it's classes:

use Doctrine\ORM\Cache;

throw new CacheException('Failed to cache');

More at http://php.net/manual/en/language.namespaces.importing.php

Waldz
  • 104
  • 6
  • But what is the point in importing packages when I can just use the full namespace, given in your example - `throw new Doctrine\ORM\Cache\CacheException('Failed to cache');` – php_nub_qq Oct 04 '14 at 12:40
  • My advice is not to use FQCN - better import once and use short names. And code is shorter & more readable. – Waldz Oct 04 '14 at 12:53